栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

使用HttpClient远程访问url地址

Java 更新时间:发布时间: 百科书网 趣学号

文章目录
  • 使用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 map=new HashMap();
        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;



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 map=new HashMap();
        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 map=new HashMap();
        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/

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/273152.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号