Java工具类HttpComponents之HttpClient,使用HttpClient发送POST/GET请求

添加依赖


<dependency>
    <groupId>org.apache.httpcomponentsgroupId>
    <artifactId>httpclientartifactId>
    <version>4.5.2version>
dependency>


<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>1.2.47version>
dependency>

以json格式参数发送post请求

/**
 * 发送json格式的post请求
 * @param url 请求地址,如:https://www.baidu.com/insterface
 * @param headerMap 请求头,以map型式拼装
 * @param jsonObject fastjson中的JSONObject对象
 * @return String
 */
public static String sendJsonPost(String url, Map<String, String> headerMap, JSONObject jsonObject) {

    // 创建httpClient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    // 创建HttpPost对象
    HttpPost post = new HttpPost(url);

    // 拼装Header
    if (headerMap != null) {
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            post.setHeader(entry.getKey(), entry.getValue());
        }
    }

    // 创建接收响应对象
    CloseableHttpResponse response = null;
    // 进行请求
    try {
        // 设置参数
        StringEntity s = new StringEntity(jsonObject.toString());
        s.setContentType("application/json");
        s.setContentEncoding("UTF-8");
        post.setEntity(s);
        // 发送请求,获取响应
        response = httpClient.execute(post);
        System.out.println(response);
        // 解析响应数据
        if(response != null) {
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        }
        // 捕获异常
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            // 先关闭httpClient
            httpClient.close();
            // 再关闭response
            if(response != null) response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

以userId=333&name=777的字符串参数发送post请求

/**
 * 发送String格式的post请求
 * @param url 请求地址,如:https://www.baidu.com/insterface
 * @param headerMap 请求头,以map型式拼装
 * @param param userId=111&name=777这种格式的字符串
 * @return String
 */
public static String sendStringPost(String url, Map<String, String> headerMap, String param) {

    // 创建httpClient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    // 创建HttpPost对象
    HttpPost post = new HttpPost(url);

    // 拼装Header
    if (headerMap != null) {
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            post.setHeader(entry.getKey(), entry.getValue());
        }
    }
    // 将map参数转成uri参数字符串格式
//        String param = buildMap(map);

    // 创建接收响应对象
    CloseableHttpResponse response = null;
    // 进行请求
    try {
        // 发送请求
        post.setEntity(new StringEntity(param,"UTF-8"));
        // 获取响应
        response = httpClient.execute(post);
        // 解析响应数据
        if(response != null) {
//            if(response != null && response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        }
    } catch (IOException e) {   // 处理异常
        e.printStackTrace();
    }finally {
        try {
            // 先关闭httpClient
            httpClient.close();
            // 再关闭response
            if(response != null) response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

你可能感兴趣的:(Java,java,json,http,post,ajax)