package cn.duoceshi.springbootdemo.utils;
import cn.duoceshi.springbootdemo.Enum.CodeEnum;
import cn.duoceshi.springbootdemo.model.HttpClientResponse;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
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.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
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.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 2023-8-30号 多测师
* 封装get、键值对格式的post、json格式的post请求的工具方法
*/
public class HttpClientUtils {
public static HttpClientResponse doGet(String url, Map<String, String> param){
return doGet(url, param, null);
}
public static HttpClientResponse doGet(String url, Map<String, String> param, Map<String, String> headers) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
packageHeader(httpGet, headers);
response = getHttpClientResponse(httpclient, httpGet);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
private static HttpClientResponse getHttpClientResponse(CloseableHttpClient httpclient, HttpRequestBase httpBase) throws IOException {
// 执行请求
CloseableHttpResponse response = httpclient.execute(httpBase);
// 封装成自定义的响应对象
if (response == null){
return HttpClientResponse.builder().status(CodeEnum.EXCEPTION.getCode()).build(); //响应为空返回500
}
int status = response.getStatusLine().getStatusCode();
Header[] headers = response.getAllHeaders();
Map<String, String> map = new HashMap<>();
for (Header header : headers){
map.put(header.getName(), header.getValue());
}
String body = EntityUtils.toString(response.getEntity(), "UTF-8");
return HttpClientResponse.builder()
.status(status)
.headers(map)
.body(body).build();
}
public static HttpClientResponse doPost(String url, Map<String, String> param){
return doPost(url, param, null);
}
/**
* 表单类型的post
*
* @param url
* @param param
* @return
*/
public static HttpClientResponse doPost(String url, Map<String, String> param, Map<String, String> headers) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpClientResponse response = null;
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 添加header
packageHeader(httpPost, headers);
// 执行http请求
response = getHttpClientResponse(httpClient, httpPost);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return response;
}
public static HttpClientResponse doPostJson(String url, String json){
return doPostJson(url, json, null);
}
/**
* json类型的post接口
* @param url
* @param json
* @return
*/
public static HttpClientResponse doPostJson(String url, String json, Map<String, String> headers) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpClientResponse response = null;
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 添加header
packageHeader(httpPost, headers);
// 执行http请求
response = getHttpClientResponse(httpClient, httpPost);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return response;
}
private static void packageHeader(HttpRequestBase httpBase, Map<String, String> headers) {
//如果headers不为空、拿到键值对并且设置到请求头当中去
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpBase.setHeader(entry.getKey(), entry.getValue());
}
}
}
}