使用org.apache.httpcomponents自己再次封装一个Http工具类。
代码实现
<!-- httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<!-- httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.12</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
import com.alibaba.fastjson.JSON;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
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.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.util.Map;
/**
* @Author: zhs
* @Date: 2021/5/20 10:32
* @Description:
* 使用HttpClient发送请求
*/
public class HttpUtils {
/**
* 发送get请求
* @param url 请求路径
* @param urlParam url参数 ?id=1&name=2
* @param headers 请求头
* @return
*/
public static String httpGet(String url, Map<String,Object> urlParam, Header... headers){
//1.解析参数
String param = HttpParamUtils.httpGetParam(urlParam);
//2.创建client与response
CloseableHttpClient client = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
//3.创建httpGet请求
HttpGet httpGet = new HttpGet(url + param);
//4.设置请求头
httpGet.setHeaders(headers);
try {
//5.发送请求
response = client.execute(httpGet);
//6.获得返回内容
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity, "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(client != null){
client.close();
}
if(response != null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* 发送post请求
* @param url 请求路径
* @param urlParam url参数 ?id=1&name=2
* @param bodyParam 请求体参数
* @param headers 请求头
* @return
*/
public static String httpPost(String url, Map<String,Object> urlParam ,Object bodyParam, Header... headers){
//1.创建client与response
CloseableHttpClient client = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
//2.创建httpPost请求
if(urlParam != null){
url = url + HttpParamUtils.httpGetParam(urlParam);
}
HttpPost httpPost = new HttpPost(url);
//3.设置参数
if(bodyParam != null){
StringEntity entity = new StringEntity(JSON.toJSONString(bodyParam), "UTF-8");
httpPost.setEntity(entity);
}
//4.设置请求头
httpPost.setHeaders(headers);
try {
//5.发送请求
response = client.execute(httpPost);
//6.获得返回内容
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity, "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(client != null){
client.close();
}
if(response != null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* 以post请求的形式发送byte[] 文件
* @param url 请求路径
* @param file 发送的文件
* @param urlParam url参数 ?id=1&name=2
* @param bodyParam 请求体参数
* @param headers 请求头
* @return
*/
public static String httpPostBinaryFile(String url, File file , Map<String,Object> urlParam ,Object bodyParam, Header... headers){
//1.创建client与response
CloseableHttpClient client = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;
//2.创建httpPost请求
if(urlParam != null){
url = url + HttpParamUtils.httpGetParam(urlParam);
}
HttpPost httpPost = new HttpPost(url);
//3.设置参数
if(file == null){
throw new NullPointerException();
}else{
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("video",file, ContentType.MULTIPART_FORM_DATA,file.getName());
httpPost.setEntity(builder.build());
}
if(bodyParam != null){
StringEntity entity = new StringEntity(JSON.toJSONString(bodyParam), "UTF-8");
httpPost.setEntity(entity);
}
//4.设置请求头
httpPost.setHeaders(headers);
try {
//5.发送请求
response = client.execute(httpPost);
//6.获得返回内容
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity, "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(client != null){
client.close();
}
if(response != null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
}
import java.util.HashMap;
import java.util.Map;
/**
* @Author: zhs
* @Date: 2021/5/20 10:40
* @Description:
*/
public class HttpParamUtils {
public static String httpGetParam(Map<String,Object> map){
if(map != null && !map.isEmpty()){
String param = "?";
for(String key:map.keySet()){
param += key + "=" + map.get(key) + "&";
}
return param.substring(0,param.length()-1);
}
return "";
}
public static void main(String[] args) {
Map<String,Object> map = new HashMap<>();
map.put("key","aaaa");
System.err.println("https://xxxx.com" + httpGetParam(map));
}
}