package com.sinata.rest.core.easemob;
|
|
import com.alibaba.fastjson.JSONObject;
|
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.methods.*;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.nio.charset.Charset;
|
|
/**
|
* http请求工具类
|
*
|
* @author: KingKong
|
* @create: 2018-09-12 16:24
|
**/
|
public class HttpRequestUtil {
|
|
private static Logger log = LoggerFactory.getLogger(EasemobApi.class);
|
|
private static CloseableHttpClient httpClient;
|
|
static {
|
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
|
cm.setMaxTotal(100);
|
cm.setDefaultMaxPerRoute(20);
|
cm.setDefaultMaxPerRoute(50);
|
httpClient = HttpClients.custom().setConnectionManager(cm).build();
|
}
|
|
public static String get(String url) {
|
CloseableHttpResponse response = null;
|
BufferedReader in = null;
|
String result = "";
|
try {
|
HttpGet httpGet = new HttpGet(url);
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
|
httpGet.setConfig(requestConfig);
|
httpGet.setConfig(requestConfig);
|
httpGet.addHeader("Content-type", "application/json; charset=utf-8");
|
httpGet.setHeader("Accept", "application/json");
|
log.info("发出GET请求信息:{}", JSONObject.toJSONString(httpGet));
|
response = httpClient.execute(httpGet);
|
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
StringBuffer sb = new StringBuffer("");
|
String line = "";
|
String NL = System.getProperty("line.separator");
|
while ((line = in.readLine()) != null) {
|
sb.append(line + NL);
|
}
|
in.close();
|
result = sb.toString();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (null != response) {
|
response.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
public static String post(String url, String jsonString, String access_token) {
|
CloseableHttpResponse response = null;
|
BufferedReader in = null;
|
String result = "";
|
try {
|
HttpPost httpPost = new HttpPost(url);
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
|
httpPost.setConfig(requestConfig);
|
httpPost.setConfig(requestConfig);
|
httpPost.addHeader("Content-type", "application/json; charset=utf-8");
|
httpPost.setHeader("Accept", "application/json");
|
if (access_token != null) {
|
httpPost.setHeader("Authorization", "Bearer " + access_token);
|
}
|
httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
|
log.debug("发出POST请求信息:{}", JSONObject.toJSONString(httpPost));
|
response = httpClient.execute(httpPost);
|
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
StringBuffer sb = new StringBuffer("");
|
String line = "";
|
String NL = System.getProperty("line.separator");
|
while ((line = in.readLine()) != null) {
|
sb.append(line + NL);
|
}
|
in.close();
|
result = sb.toString();
|
log.debug("收到响应信息:{}", result);
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (null != response) {
|
response.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
public static String delete(String url, String access_token) {
|
CloseableHttpResponse response = null;
|
BufferedReader in = null;
|
String result = "";
|
try {
|
HttpDelete httpDelete = new HttpDelete(url);
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
|
httpDelete.setConfig(requestConfig);
|
httpDelete.setConfig(requestConfig);
|
httpDelete.addHeader("Content-type", "application/json; charset=utf-8");
|
httpDelete.setHeader("Accept", "application/json");
|
if (access_token != null) {
|
httpDelete.setHeader("Authorization", "Bearer " + access_token);
|
}
|
log.info("发出DELETE请求信息:{}", JSONObject.toJSONString(httpDelete));
|
response = httpClient.execute(httpDelete);
|
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
StringBuffer sb = new StringBuffer("");
|
String line = "";
|
String NL = System.getProperty("line.separator");
|
while ((line = in.readLine()) != null) {
|
sb.append(line + NL);
|
}
|
in.close();
|
result = sb.toString();
|
log.debug("收到响应信息:{}", result);
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (null != response) {
|
response.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
|
public static String wxPost(String url, String xmlString) {
|
CloseableHttpResponse response = null;
|
BufferedReader in = null;
|
String result = "";
|
try {
|
HttpPost httpPost = new HttpPost(url);
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
|
httpPost.setConfig(requestConfig);
|
httpPost.setConfig(requestConfig);
|
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
httpPost.addHeader("Accept", "application/xml");
|
httpPost.setEntity(new StringEntity(xmlString, "utf-8"));
|
log.debug("微信下单发出POST请求信息:{}", JSONObject.toJSONString(httpPost));
|
response = httpClient.execute(httpPost);
|
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
StringBuffer sb = new StringBuffer("");
|
String line = "";
|
String NL = System.getProperty("line.separator");
|
while ((line = in.readLine()) != null) {
|
sb.append(line + NL);
|
}
|
in.close();
|
result = sb.toString();
|
log.debug("收到响应信息:{}", result);
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (null != response) {
|
response.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
public static String put(String url, String param) {
|
CloseableHttpResponse response = null;
|
BufferedReader in = null;
|
String result = "";
|
try {
|
HttpPut http = new HttpPut(url);
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
|
http.setConfig(requestConfig);
|
http.setConfig(requestConfig);
|
http.addHeader("Content-type", "application/json; charset=utf-8");
|
http.setHeader("Accept", "application/json");
|
http.setEntity(new StringEntity(param, "utf-8"));
|
log.info("发出put的url:{}", url);
|
log.info("发出put请求参数信息:{}", param);
|
response = httpClient.execute(http);
|
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
StringBuffer sb = new StringBuffer("");
|
String line = "";
|
String NL = System.getProperty("line.separator");
|
while ((line = in.readLine()) != null) {
|
sb.append(line + NL);
|
}
|
in.close();
|
result = sb.toString();
|
log.info("收到PUT响应信息:{}", result);
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (null != response) {
|
response.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
}
|