|
@@ -18,41 +18,20 @@ public class HttpClientUtil {
|
|
private static final HttpClient client = HttpClients.createDefault();
|
|
private static final HttpClient client = HttpClients.createDefault();
|
|
|
|
|
|
public synchronized static String put(String url, String content) throws Exception {
|
|
public synchronized static String put(String url, String content) throws Exception {
|
|
- String result = put(url, content, 300000);
|
|
|
|
|
|
+ String result = put(url, content, 300000, null);
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- public synchronized static String put(String url, String content, Integer timeout) throws Exception {
|
|
|
|
|
|
+ public synchronized static String put(String url, String content, Integer timeout, Map<String, String> headerMap) throws Exception {
|
|
HttpPut httpost = new HttpPut(url);
|
|
HttpPut httpost = new HttpPut(url);
|
|
- if (timeout != null) {
|
|
|
|
- RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
|
|
- .setSocketTimeout(timeout).build();
|
|
|
|
- httpost.setConfig(requestConfig);
|
|
|
|
|
|
+ if (headerMap != null) {
|
|
|
|
+ for (String key : headerMap.keySet()) {
|
|
|
|
+ httpost.setHeader(key, headerMap.get(key));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- StringEntity entity = new StringEntity(content, "UTF-8");
|
|
|
|
- httpost.setEntity(entity);
|
|
|
|
- entity.setContentType("application/json");
|
|
|
|
- HttpResponse httpResponse = client.execute(httpost);
|
|
|
|
- InputStream is = httpResponse.getEntity().getContent();
|
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
- int b = -1;
|
|
|
|
- while ((b = is.read()) != -1) {
|
|
|
|
- baos.write(b);
|
|
|
|
|
|
+ if (timeout == null) {
|
|
|
|
+ timeout = 300000;
|
|
}
|
|
}
|
|
- baos.close();
|
|
|
|
- is.close();
|
|
|
|
-
|
|
|
|
- String result = baos.toString("UTF-8");
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public synchronized static String post(String url, String content) throws Exception {
|
|
|
|
- String result = post(url, content, 300000);
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public synchronized static String post(String url, String content, Integer timeout) throws Exception {
|
|
|
|
- HttpPost httpost = new HttpPost(url);
|
|
|
|
if (timeout != null) {
|
|
if (timeout != null) {
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
.setSocketTimeout(timeout).build();
|
|
.setSocketTimeout(timeout).build();
|
|
@@ -76,12 +55,20 @@ public class HttpClientUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public synchronized static String get(String url) throws Exception {
|
|
public synchronized static String get(String url) throws Exception {
|
|
- String result = get(url, 300000);
|
|
|
|
|
|
+ String result = get(url, 300000, null);
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- public synchronized static String get(String url, Integer timeout) throws Exception {
|
|
|
|
|
|
+ public synchronized static String get(String url, Integer timeout, Map<String, String> headerMap) throws Exception {
|
|
HttpGet httpget = new HttpGet(url);
|
|
HttpGet httpget = new HttpGet(url);
|
|
|
|
+ if (headerMap != null) {
|
|
|
|
+ for (String key : headerMap.keySet()) {
|
|
|
|
+ httpget.setHeader(key, headerMap.get(key));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (timeout == null) {
|
|
|
|
+ timeout = 300000;
|
|
|
|
+ }
|
|
if (timeout != null) {
|
|
if (timeout != null) {
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
.setSocketTimeout(timeout).build();
|
|
.setSocketTimeout(timeout).build();
|
|
@@ -103,23 +90,27 @@ public class HttpClientUtil {
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- public synchronized static String post(String url, String content, Map<String, String> headParam) throws Exception {
|
|
|
|
- return post(url, content, headParam, 300000);
|
|
|
|
|
|
+ public synchronized static String post(String url, String content) throws Exception {
|
|
|
|
+ String result = post(url, content, 300000, null);
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
|
|
- public synchronized static String post(String url, String content, Map<String, String> headParam, Integer timeout) throws Exception {
|
|
|
|
|
|
+ public synchronized static String post(String url, String content, Integer timeout, Map<String, String> headerMap) throws Exception {
|
|
HttpPost httpost = new HttpPost(url);
|
|
HttpPost httpost = new HttpPost(url);
|
|
|
|
+ if (headerMap != null) {
|
|
|
|
+ for (String key : headerMap.keySet()) {
|
|
|
|
+ httpost.setHeader(key, headerMap.get(key));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (timeout == null) {
|
|
|
|
+ timeout = 300000;
|
|
|
|
+ }
|
|
if (timeout != null) {
|
|
if (timeout != null) {
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
.setSocketTimeout(timeout).build();
|
|
.setSocketTimeout(timeout).build();
|
|
httpost.setConfig(requestConfig);
|
|
httpost.setConfig(requestConfig);
|
|
}
|
|
}
|
|
StringEntity entity = new StringEntity(content, "UTF-8");
|
|
StringEntity entity = new StringEntity(content, "UTF-8");
|
|
- if (headParam != null && headParam.size() > 0) {
|
|
|
|
- headParam.forEach((key, value) -> {
|
|
|
|
- httpost.addHeader(key, value);
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
httpost.setEntity(entity);
|
|
httpost.setEntity(entity);
|
|
entity.setContentType("application/json");
|
|
entity.setContentType("application/json");
|
|
HttpResponse httpResponse = client.execute(httpost);
|
|
HttpResponse httpResponse = client.execute(httpost);
|