package com.xxxxx.util.http;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
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.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
public static String doGet(String url, Map<String, String> param, Map<String, String> header) {
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
if (null != param) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
if (null != header) {
header.forEach((key, value) -> {
httpGet.setHeader(key, value);
});
}
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null, null);
}
public static String doGet(String url, Map<String, String> param) {
return doGet(url, param, null);
}
public static String doPost(String url, Map<String, String> param, JSONObject body, Map<String, String> header) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
URIBuilder builder = new URIBuilder(url);
if (null != param) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
HttpPost httpPost = new HttpPost(uri);
if (null != header) {
header.forEach((key, value) -> {
httpPost.setHeader(key, value);
});
}
if (null != body) {
StringEntity entity = new StringEntity(body.toJSONString());
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null, null, null);
}
public static String doPost(String url, Map<String, String> param) {
return doPost(url, param, null, null);
}
public static String doPost(String url, JSONObject body) {
return doPost(url, null, body, null);
}
public static String doPostParam(String url, Map<String, String> param, Map<String, String> header) {
return doPost(url, param, null, header);
}
public static String doPost(String url, JSONObject body, Map<String, String> header) {
return doPost(url, null, body, header);
}
public static String doPost(String url, Map<String, String> param, JSONObject body) {
return doPost(url, param, body, null);
}
}