POM
<!-- Apache Http Begin -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.5</version>
</dependency>
<!-- Apache Http End -->
工具类
package cn.hzlim.hello.httpclient;
import org.apache.http.HttpEntity;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Arrays;
/**
*
* HttpClient工具类
* @title: HttpClientUtils
* @description:
*
* @author: xianyu_tf
* @date: 2019-07-12 22:53
* @version 1.0.0
*
*/
public class HttpClientUtils {
public static final String GET="get";
public static final String POST="post";
public static final String REQUEST_HEADER_CONNECTION="keep-alive";
public static final String REQUEST_HEADER_USER_AGENT="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
/**
* get请求
* @param url 请求地址
* @return
*/
public static String doGet(String url){
return createRequest(url,GET,null);
}
/**
* get请求
* @param url 请求地址
* @param cookie cookie
* @return
*/
public static String doGet(String url,String cookie){
return createRequest(url,GET,cookie);
}
/**
* post请求
* @param url 请求地址
* @param params 请求参数
* @return
*/
public static String doPost(String url,BasicNameValuePair... params){
return createRequest(url,POST,null,params);
}
/**
* post请求
* @param url 请求地址
* @param cookie cookie
* @param params 请求参数
* @return
*/
public static String doPost(String url,String cookie,BasicNameValuePair... params){
return createRequest(url,POST,cookie,params);
}
/**
* 创建请求
* @param url 请求地址
* @param requestMethod 请求方式get/post
* @param cookie cookie
* @param params 请求参数 仅限于post
* @return
*/
private static String createRequest(String url,String requestMethod,String cookie,BasicNameValuePair... params){
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = null;
//请求方式
HttpPost httpPost = null;
HttpGet httpGet = null;
//响应
CloseableHttpResponse httpResponse = null;
try {
if (GET.equals(requestMethod)){
httpGet = new HttpGet(url);
httpGet.setHeader("Connection", REQUEST_HEADER_CONNECTION);
httpGet.setHeader("Cookie", cookie);
httpGet.setHeader("User-Agent", REQUEST_HEADER_USER_AGENT);
httpResponse = httpClient.execute(httpGet);
}else if(POST.equals(requestMethod)){
httpPost = new HttpPost(url);
httpPost.setHeader("Connection", REQUEST_HEADER_CONNECTION);
httpPost.setHeader("Cookie", cookie);
httpPost.setHeader("User-Agent", REQUEST_HEADER_USER_AGENT);
//判断是否有参数
if(params != null && params.length > 0){
httpPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(params), "UTF-8"));
}
httpResponse = httpClient.execute(httpPost);
}
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
}catch (IOException e){
e.printStackTrace();
}finally {
//关闭连接
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}