该方法为Apache HttpClient 4.5以上的版本支持,在官网有明确的说明。
对比以前的方式,其优点是代码更简洁,同时为线程安全的。仅举一个最简单的post栗子
JAR包信息:
org.apache.httpcomponents
httpclient
4.5.1
package utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* Description:
* Http请求工具类
*
* @author
* Date: 2017-11-01
* Time: 10:24
*/
public class HttpClientUtil {
/**
* @return String
* 响应体
*/
public String jsonPostHttp(JSONObject jsonObject, String url) throws IOException {
String response = Request.Post(url).bodyString(jsonObject.toString(), ContentType.APPLICATION_JSON).execute().retu