import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* @author :ChatGPT 宗
* @date :Created in 2023/3/14 16:29
* @description: HttpClient封装类
*/
public class MyHttpClientUtil {
/**
* 创建HttpClient
*/
private static final HttpClient CLIENT = HttpClientBuilder.create().build();
/**
* GET
*
* @param url
* @return
* @throws IOException
*/
public static String doGet(String url) throws IOException {
return doGet(url, null);
}
/**
* GET(重载方法,带headers参数)
*
* @param url
* @param headers
* @return
* @throws IOException
*/
public static String doGet(String url, Map<String, String> headers) throws IOException {
HttpGet httpGet = new HttpGet(url);
setHeaders(httpGet, headers); // 设置headers
HttpResponse response = CLIENT.execute(httpGet); // 执行请求
return handleResponse(response); // 处理响应
}
/**
* POST
*
* @param url
* @param jsonBody
* @return
* @throws IOException
*/
public static String doPost(String url, String jsonBody) throws IOException {
return doPost(url, jsonBody, null);
}
/**
* POST(重载方法,带headers参数)
*
* @param url
* @param jsonBody
* @param headers
* @return
* @throws IOException
*/
public static String doPost(String url, String jsonBody, Map<String, String> headers) throws IOException {
HttpPost httpPost = new HttpPost(url);
setHeaders(httpPost, headers); // 设置headers
httpPost.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8)); // 设置请求体
HttpResponse response = CLIENT.execute(httpPost); // 执行请求
return handleResponse(response); // 处理响应
}
/**
* PUT
*
* @param url
* @param jsonBody
* @return
* @throws IOException
*/
public static String doPut(String url, String jsonBody) throws IOException {
return doPut(url, jsonBody, null);
}
/**
* PUT(重载方法,带headers参数)
*
* @param url
* @param jsonBody
* @param headers
* @return
* @throws IOException
*/
public static String doPut(String url, String jsonBody, Map<String, String> headers) throws IOException {
HttpPut httpPut = new HttpPut(url);
setHeaders(httpPut, headers); // 设置headers
httpPut.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8)); // 设置请求体
HttpResponse response = CLIENT.execute(httpPut); // 执行请求
return handleResponse(response); // 处理响应
}
/**
* DELETE
*
* @param url
* @return
* @throws IOException
*/
public static String doDelete(String url) throws IOException {
return doDelete(url, null);
}
/**
* DELETE(重载方法,带headers参数)
*
* @param url
* @param headers
* @return
* @throws IOException
*/
public static String doDelete(String url, Map<String, String> headers) throws IOException {
HttpDelete httpDelete = new HttpDelete(url);
setHeaders(httpDelete, headers); // 设置headers
HttpResponse response = CLIENT.execute(httpDelete); // 执行请求
return handleResponse(response); // 处理响应
}
/**
* 设置headers的方法
*
* @param httpRequest
* @param headers
*/
private static void setHeaders(Object httpRequest, Map<String, String> headers) {
if (headers != null) {
headers.forEach((key, value) -> {
if (httpRequest instanceof HttpGet) {
((HttpGet) httpRequest).addHeader(key, value);
} else if (httpRequest instanceof HttpPost) {
((HttpPost) httpRequest).addHeader(key, value);
} else if (httpRequest instanceof HttpPut) {
((HttpPut) httpRequest).addHeader(key, value);
} else if (httpRequest instanceof HttpDelete) {
((HttpDelete) httpRequest).addHeader(key, value);
}
});
}
}
/**
* 处理响应的方法,将响应转换成字符串返回
*
* @param response HttpResponse对象
* @return 响应内容字符串
* @throws IOException
*/
private static String handleResponse(HttpResponse response) throws IOException {
// 获取响应实体
HttpEntity entity = response.getEntity();
if (entity == null) {
// 如果响应实体为空,则返回空字符串
return "";
}
// 将响应实体转换成字符串
String responseBody = EntityUtils.toString(entity, StandardCharsets.UTF_8);
// 关闭响应实体的输入流
EntityUtils.consume(entity);
// 返回响应内容字符串
return responseBody;
}
}
HttpClient封装类
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...