HttpClient调用后台接口(用CloseableHttpClient发送get/post请求)

前言

在没有页面的情况下来获取接口返回的数据(一般都是为JSON),我们可以通过一些工具模拟HTTP请求

服务端模拟HTTP请求
通过JAVA代码进行HTTP请求的发送

引入依赖

<dependency>
<groupId>org.apache.httpcomponents</groupId> 
<artifactId>httpclient</artifactId> 
<version>4.5.6</version>
</dependency>

<dependency> 
<groupId>com.alibaba</groupId>
 <artifactId>fastjson</artifactId>
  <version>1.2.73</version>
</dependency>

代码

    public class HttpClientUtil {
    //参数加在页面的get请求,用?和&衔接参数
    public static void httpGet1() throws IOException, URISyntaxException {
        //建立连接请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建post请求
        HttpGet httpGet = new HttpGet("http://10.143.88.87/sap/get_bom_all?modelName=A90AI00B2-T00051&plant=WEAS");
        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity!=null){
            System.out.println("接收到的响应信息:--------"+ EntityUtils.toString(entity,"UTF-8"));
        }
        //资源释放
        response.close();
        httpClient.close();
    }

    //参数加在页面的get请求,用BasicNameValuePair接参数,前面是参数,后面是值。
    public static void httpGet2() throws IOException, URISyntaxException {
        //get请求
        URIBuilder uri = new URIBuilder("http://10.143.88.87/sap/get_bom_all");
        //get请求带参数
        List<NameValuePair> list = new LinkedList<>();
        BasicNameValuePair param1 = new BasicNameValuePair("modelName", "A90AI00B2-T00051");
        BasicNameValuePair param2 = new BasicNameValuePair("plant", "WEAS");
        list.add(param1);
        list.add(param2);
        uri.setParameters(list);
        HttpGet httpGet = new HttpGet(uri.build());
        //建立连接请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println("接收到的响应信息:--------" + EntityUtils.toString(entity, "UTF-8"));
        }
        //资源释放
        response.close();
        httpClient.close();
    }

//post请求,后台接收的参数是application/x-www-form-urlencoded是浏览器默认的编码格式。对于Get请求,是将参数转换?key=value&key=value格式,连接到url后,如果是POST,则x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…)
    public static void httpPost1() throws IOException {
        //建立连接请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建post请求
        HttpPost httpPost = new HttpPost("http://10.83.16.208/AegisWebAPIV02/API/Aegist2/WebSerAegis101V01");


        String json = "={ "+
                "           \"CmdNo\":\"31105\",\n" +
                "            \"account\":\"\",\n" +
                "            \"password\":\"\",\n" +
                "            \"tmpdate\":\"\",\n" +
                "            \"ProcID\":\"\",\n" +
                "\"d1\":\"{\\\"Table1\\\":[{\\\"SysNo\\\":\\\"\\\",\\\"AegType\\\":\\\"103\\\",\\\"ID\\\":\\\"\\\",\\\"APassword\\\":\\\"\\\",\\\"SetDateTime\\\":\\\"\\\",\\\"AegPS\\\":\\\"\\\",\\\"APPDevice\\\":\\\"\\\",\\\"R1\\\":\\\"11210136459\\\",\\\"R2\\\":\\\"90AI0052-M00160\\\",\\\"R3\\\":\\\"R3\\\",\\\"R4\\\":\\\"R4\\\",\\\"R5\\\":\\\"R5\\\",\\\"R6\\\":\\\"R6\\\",\\\"R7\\\":\\\"R7\\\",\\\"R8\\\":\\\"R8\\\",\\\"R9\\\":\\\"R9\\\",\\\"R10\\\":\\\"R10\\\"}]}\",            \"d2\":\"TEST\",\n" +
                "            \"d3\":\"\",\n" +
                "            \"d4\":\"\",\n" +
                "            \"d5\":\"\"\n" +
                "}";

        StringEntity requestEntity = new StringEntity(json,"utf-8");


        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");


        //讲实体封装到请求当中
        httpPost.setEntity(requestEntity);
        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if (entity!=null){
            System.out.println("接收到的响应信息:--------"+ EntityUtils.toString(entity,"UTF-8"));
        }
        //资源释放
        response.close();
        httpClient.close();
    }

//post请求,后台接收的参数是"application/json;charset=utf-8"
    public static void httpPost2() throws IOException {
        //建立连接请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建post请求
        HttpPost httpPost = new HttpPost("http://10.143.88.86:8099/sys/login2");


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserCode","F2848001");
        jsonObject.put("Password","abcd1234");
        jsonObject.put("StationID","SOCSCREW11");
        jsonObject.put("Lang","zh_tw");
        jsonObject.put("SiteCode","S001");
        jsonObject.put("BUCode","B201");

        StringEntity requestEntity = new StringEntity(jsonObject.toString(),"utf-8");

        httpPost.setHeader("Content-Type", "application/json;charset=utf-8");

        //讲实体封装到请求当中
        httpPost.setEntity(requestEntity);
        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if (entity!=null){
            System.out.println("接收到的响应信息:--------"+ EntityUtils.toString(entity,"UTF-8"));
        }
        //资源释放
        response.close();
        httpClient.close();
    }

    public static void main(String[] args) throws IOException, URISyntaxException {
       httpPost2();
    }

}

注:

如果PostMethod提交的是中文字符,需要加上相应的编码格式: post.setRequestHeader(“Content-Type”,“application/x-www-form-urlencoded;charset=utf-8”);
如果GetMethod提交的参数有中文字符,需要先转换成utf-8格式:String param = “enterpriseName=”+ URLEncoder.encode(“中国移动”, “utf-8”); HttpUtil.doGet(param, url);

maven依赖说明
commons-httpclient 是 apache-commons 项目下的一个子项目,后来被 HttpComponents 取代,后者提供了更好的性能和更大的灵活性。

commons-httpclient的GAV地址为

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>

其最新版本为3.1,且已经不再更新;

HttpComponents的GAV地址为
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HttpClientJava中的一个开源库,用于支持HTTP协议的客户端编程。它是一个用于发送HTTP请求和接收HTTP响应的包装工具类。HttpClient可以被用于执行GET和POST请求HTTP方法。走看看是一个基于Web的应用程序,包含了各种常见的网站功能,如搜索、资讯、体育、财经、购物等多个频道。下面将分别介绍HttpClient发送GET和POST请求时的一些重要知识点。 HttpClient发送GET请求时,需要构造一个HttpGet对象,并指定请求的URL。调用HttpClient.execute方法,并且将HttpGet对象传递给该方法。接下来,HttpClient发送GET请求到指定的URL,然后将响应内容作为一个HttpResponse对象返回给程序。可以从HttpResponse对象中获取响应状态、响应头和响应体等信息。 HttpClient发送POST请求时,需要首先构造一个HttpPost对象,并指定请求的URL。调用HttpPost.setEntity方法来设置请求体内容,然后调用HttpClient.execute方法,并将HttpPost对象传递给该方法。接下来,HttpClient会将POST请求数据发送到指定的URL,然后将响应内容作为一个HttpResponse对象返回给程序。与GET请求相似,可以从HttpResponse对象中获取响应状态、响应头和响应体等信息。 总的来说,HttpClient是一个十分强大和方便的网络编程工具类,可以方便地实现HTTP请求和响应的处理。可以根据自己的需求选择GET和POST请求发送,然后获取响应内容和各种信息。使用HttpClient能够简化开发,提高编程效率,是Java网络编程开发中非常重要的一种库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叶孤崖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值