使用HttpClient远程访问url地址

使用HttpClient远程访问url地址

草书

首先需要远程的url地址,其次需要传递的参数,代码如下:

public class WorkDayClient {
    public static void doPostTestTwo() {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Post请求
        HttpPost httpPost = new HttpPost("http://dingtalk.testk8s.tsign.cn/workday/deadline/");
        //写参数
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("days",4);
        map.put("startTime","2021-10-01 21:12:30");
        //使用json将参数对象转换成json对象
        String str=JSON.toJSONString(map);
        StringEntity en = new StringEntity(str, "UTF-8");

        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(en);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        doPostTestTwo();
    }
}

远程调用的结果,如下图:

在这里插入图片描述

我们上面的这个例子,是通过http调用远程的服务器中的方法的,从上图可以看出,调用成功。

具体代码工程

具体的代码工程已经上传到了自己的码云中去了。

使用http访问远程的服务器上的接口的时候,首先需要导入与http有关的依赖,然后因为http牵涉到传递json字符串参数,所以还必须要导入fastjson处理字符串的依赖,如下图:

在这里插入图片描述

然后写具体的代码,如下图:

在这里插入图片描述

具体代码如下:

package org.eqianbao.utils;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;


/**
 * @Date 2021/9/27 17:38
 * @Author 望轩
 */
public class WorkDayUtil {
    //远程调用计算截止日期的接口
    public static String getDeadLine(String days,String startTime) {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Post请求
        HttpPost httpPost = new HttpPost("http://dingtalk.testk8s.tsign.cn/workday/deadline/");
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("days",days);
        map.put("startTime",startTime);
        String str= JSON.toJSONString(map);
        StringEntity en = new StringEntity(str, "UTF-8");
        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(en);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            return EntityUtils.toString(responseEntity);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //远程调用计算两个时间的时间段内所有的工作日(单位s)
    public static String getWorkDay(String startTime,String endTime){
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Post请求
        HttpPost httpPost = new HttpPost("http://dingtalk.testk8s.tsign.cn/workday/elapsedTime/");
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        String str=JSON.toJSONString(map);
        StringEntity en = new StringEntity(str, "UTF-8");
        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(en);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            return EntityUtils.toString(responseEntity);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getDeadLine("4","2021-10-01 21:10:34"));
        System.out.println(getWorkDay("2021-10-01 09:30:00","2021-10-08 09:30:00"));
    }
}

效果如下图:

在这里插入图片描述

码云相关地址:https://gitee.com/xuanyuanzy/httpclient/

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr-X~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值