优化
This commit is contained in:
parent
486d0d80c6
commit
462da67a04
@ -16,6 +16,8 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public class WDHttpClientUtils {
|
public class WDHttpClientUtils {
|
||||||
|
|
||||||
|
private static final boolean printLog = false;
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class);
|
private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,7 +61,9 @@ public class WDHttpClientUtils {
|
|||||||
* 封装get请求方式的处理
|
* 封装get请求方式的处理
|
||||||
*/
|
*/
|
||||||
private static String doGet(String url, CloseableHttpClient httpClient) throws Exception {
|
private static String doGet(String url, CloseableHttpClient httpClient) throws Exception {
|
||||||
log.info("HGet请求url={}", url);
|
if (printLog) {
|
||||||
|
log.info("HGet请求url={}", url);
|
||||||
|
}
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
return execute(httpClient, httpGet);
|
return execute(httpClient, httpGet);
|
||||||
}
|
}
|
||||||
@ -68,8 +72,10 @@ public class WDHttpClientUtils {
|
|||||||
* 封装post请求方式的处理
|
* 封装post请求方式的处理
|
||||||
*/
|
*/
|
||||||
private static String doPost(CloseableHttpClient httpClient, String url, String params) throws Exception {
|
private static String doPost(CloseableHttpClient httpClient, String url, String params) throws Exception {
|
||||||
log.info("Post请求url:{}", url);
|
if (printLog) {
|
||||||
log.info("Post请求params:{}", params);
|
log.info("Post请求url:{}", url);
|
||||||
|
log.info("Post请求params:{}", params);
|
||||||
|
}
|
||||||
HttpPost httpPost = new HttpPost(url);
|
HttpPost httpPost = new HttpPost(url);
|
||||||
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
|
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
|
||||||
httpPost.setEntity(new StringEntity(params, "utf-8"));
|
httpPost.setEntity(new StringEntity(params, "utf-8"));
|
||||||
@ -80,7 +86,9 @@ public class WDHttpClientUtils {
|
|||||||
* 封装post请求方式的处理
|
* 封装post请求方式的处理
|
||||||
*/
|
*/
|
||||||
private static String doPost(CloseableHttpClient httpClient, String url) throws Exception {
|
private static String doPost(CloseableHttpClient httpClient, String url) throws Exception {
|
||||||
log.info("Post请求url:{}", url);
|
if (printLog) {
|
||||||
|
log.info("Post请求url:{}", url);
|
||||||
|
}
|
||||||
HttpPost httpPost = new HttpPost(url);
|
HttpPost httpPost = new HttpPost(url);
|
||||||
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
|
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
|
||||||
return execute(httpClient, httpPost);
|
return execute(httpClient, httpPost);
|
||||||
@ -95,15 +103,21 @@ public class WDHttpClientUtils {
|
|||||||
try {
|
try {
|
||||||
response = httpClient.execute(requestBase);
|
response = httpClient.execute(requestBase);
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
log.info("HttpClient响应码={}", statusCode);
|
if (printLog) {
|
||||||
|
log.info("HttpClient响应码={}", statusCode);
|
||||||
|
}
|
||||||
final int success = 200;
|
final int success = 200;
|
||||||
if (statusCode == success) {
|
if (statusCode == success) {
|
||||||
result = EntityUtils.toString(response.getEntity(), "utf-8");
|
result = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||||
} else {
|
} else {
|
||||||
log.error("HttpClient请求失败,错误码={}", statusCode);
|
if (printLog) {
|
||||||
|
log.error("HttpClient请求失败,错误码={}", statusCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("HttpClient请求异常:", e);
|
if (printLog) {
|
||||||
|
log.error("HttpClient请求异常:", e);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (null != httpClient) {
|
if (null != httpClient) {
|
||||||
httpClient.close();
|
httpClient.close();
|
||||||
@ -112,7 +126,9 @@ public class WDHttpClientUtils {
|
|||||||
response.close();
|
response.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.info("HttpClient请求结果:{}", result);
|
if (printLog) {
|
||||||
|
log.info("HttpClient请求结果:{}", result);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user