<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
package com.keda.mediaschedule.common.utils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
public static String sendGet(String url,Map<String,Object> paraMap, Map<String, String> headerMap) throws Exception{
String content = "";
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {
URIBuilder uriBuilder = new URIBuilder(url);
if (null != paraMap && paraMap.size() > 0) {
for (Map.Entry<String, Object> entry : paraMap.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded");
if (null != headerMap && headerMap.size() > 0) {
headerMap.forEach((key,value)->{
httpGet.addHeader(key, value);
});
}
httpClient = HttpClients.createDefault();
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
content = EntityUtils.toString(entity);
} finally {
if (null != httpResponse){
httpResponse.close();
}
if (null != httpClient){
httpClient.close();
}
}
return content;
}
public static String sendPost(String url, String jsonStr, Map<String, String> headerMap) throws Exception{
System.out.println(url);
String content = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(3000)
.setConnectionRequestTimeout(3000)
.setSocketTimeout(6000).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-type", "application/json;charset=utf-8");
if (null != headerMap && headerMap.size() > 0) {
headerMap.forEach((key,value)->{
httpPost.addHeader(key, value);
});
}
CloseableHttpResponse httpResponse = null;
try {
httpPost.setEntity(new StringEntity(jsonStr, "utf-8"));
httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
content = EntityUtils.toString(entity,"UTF-8");
} finally{
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
}
return content;
}
public static String sendPut(String url, String jsonStr, Map<String, String> headerMap) throws Exception{
String content = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(3000)
.setConnectionRequestTimeout(3000)
.setSocketTimeout(6000).build();
httpPut.setConfig(requestConfig);
httpPut.setHeader("Content-type", "application/json;charset=utf-8");
if (null != headerMap && headerMap.size() > 0) {
headerMap.forEach((key,value)->{
httpPut.addHeader(key, value);
});
}
CloseableHttpResponse httpResponse = null;
try {
httpPut.setEntity(new StringEntity(jsonStr,"utf-8"));
httpResponse = httpClient.execute(httpPut);
HttpEntity entity = httpResponse.getEntity();
content = EntityUtils.toString(entity,"UTF-8");
} finally {
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
}
return content;
}
public static String sendDelete(String url, Map<String,String> headerMap) throws Exception{
String content = null;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpDelete httpdelete = new HttpDelete(url);
httpdelete.setHeader("Content-type", "application/json;charset=utf-8");
if (null != headerMap && headerMap.size() > 0) {
headerMap.forEach((key,value)-> httpdelete.addHeader(key, value));
}
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpdelete);
HttpEntity entity = httpResponse.getEntity();
content = EntityUtils.toString(entity, "UTF-8");
} finally {
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
}
return content;
}
}