1、Maven项目依赖
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
2、下面有三个案列
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* @Description: httpclient帮助类
* @Date : 2019/12/30 17:26
*/
public class HttpClientUtils {
/**
* @Description: httpclient Post
* @Date : 2019/12/30 18:11
*/
public static String httpClientPost(String url, Map<String, Object> map) {
// 返回值
String result = "";
// 通过HttpPost来发送post请求
HttpPost httpPost = new HttpPost(url);
// 响应
CloseableHttpResponse response;
// 获取默认的请求客户端
try (CloseableHttpClient client = HttpClients.createDefault()) {
// 第三步:构造list集合参数
List<NameValuePair> list = new ArrayList<>();
for (String m : map.keySet()) {
BasicNameValuePair basicNameValuePair =
new BasicNameValuePair(m, map.get(m).toString());
list.add(basicNameValuePair);
}
// 第二步:我们发现Entity是一个接口,所以只能找实现类,发现实现类又需要一个集合,集合的泛型是NameValuePair类型
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
// 第一步:通过setEntity 将我们的entity对象传递过去
httpPost.setEntity(formEntity);
// 是一个中间的桥梁,在httpClient里面,是连接我们的请求与响应的一个中间桥梁,所有的请求参数都是通过HttpEntity携带过去的
// 通过client来执行请求,获取一个响应结果
response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @Description: httpclient post 封装
* @RequestType: application/json
* @Date : 2019/12/30 17:08
*/
public static String httpClientPost(String url, String jsonParams) {
// 返回值
String result = "";
// 定义请求方式
HttpPost httpPost = new HttpPost(url);
// 设置请求配置
RequestConfig requestConfig = RequestConfig.custom().
setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
httpPost.setConfig(requestConfig);
// 设置请求类型
httpPost.setHeader("Content-Type", "application/json");
// 创建httpclient客户端
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
httpPost.setEntity(new StringEntity(jsonParams, ContentType.create("application/json", "utf-8")));
HttpResponse response = httpClient.execute(httpPost);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @Description: httpclient get封装
* @Date : 2019/12/30 17:12
*/
public static String httpClientGet(String url, Map<String, Object> map) {
// 参数
String params = "";
StringBuffer sb = new StringBuffer();
// 准备拼接参数
if (Objects.nonNull(map)) {
params = url + "?";
for (Object m : map.keySet()) {
sb = sb.append("&").append(m).append(map.get(m));
}
params += sb.substring(1, sb.length());
}
// 接受返回值
String result = "";
// 创建http客户端
try (CloseableHttpClient client = HttpClients.createDefault()) {
// 采用get请求
HttpGet httpGet = new HttpGet(params);
// 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了
CloseableHttpResponse Response = client.execute(httpGet);
// HttpEntity
// 是一个中间的桥梁,在httpClient里面,是连接我们的请求与响应的一个中间桥梁,所有的请求参数都是通过HttpEntity携带过去的
// 所有的响应的数据,也全部都是封装在HttpEntity里面
HttpEntity entity = Response.getEntity();
// 通过EntityUtils 来将我们的数据转换成字符串
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}