项目总结38:使用httpcomponents: httpclient请求数据

 

Maven依赖

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

代码示例

package com.hzsun.shr.web.maintenance.util.http;

/* *
 *@Description:
 *@Author:TYJ
 *@Date: create in  2019/10/29 14:56
 */

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
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.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientImplTemp {

    /**
     *@Description 1-POST请求
     *@param  //blog:https://www.cnblogs.com/hhhshct/p/8523697.html
     *@return  java.lang.String
     *@author  TangYujie
     *@date  2019/10/29 15:34
     */
    public static String doPostWithHeaderAndJsonBody(String url, Map<String, Object> headerParamMap,String jsonBody) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        String result = "";
        // 创建httpClient实例
        httpClient = HttpClients.createDefault();
        // 创建httpPost远程连接实例
        HttpPost httpPost = new HttpPost(url);
        // 配置请求参数实例
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间
                .setConnectionRequestTimeout(35000)// 设置连接请求超时时间
                .setSocketTimeout(60000)// 设置读取数据连接超时时间
                .build();
        // 为httpPost实例设置配置
        httpPost.setConfig(requestConfig);
        // 设置请求头
        //httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.addHeader("Content-Type", "application/json");//根据实际情况设置
        Set<String> headerKeys = headerParamMap.keySet();
        for(String key : headerKeys){
            httpPost.addHeader(key,headerParamMap.get(key).toString());
        }
        // 封装body请求参数
        httpPost.setEntity(new StringEntity(jsonBody,"utf-8"));
        try {
            // httpClient对象执行post请求,并返回响应参数对象
            httpResponse = httpClient.execute(httpPost);
            // 从响应对象中获取响应内容
            HttpEntity entity = httpResponse.getEntity();
            result = EntityUtils.toString(entity);
            System.out.println("result:" + result);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != httpResponse) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

 

posted on 2019-10-30 15:05  我不吃番茄  阅读(333)  评论(0编辑  收藏  举报