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

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

前言

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

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

引入依赖

org.apache.httpcomponents 
httpclient 
4.5.6


 
com.alibaba
 fastjson
  1.2.73

代码
    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 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地址为


commons-httpclient
commons-httpclient
3.1

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

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


org.apache.httpcomponents
httpclient
4.5.5

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

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号