优化
This commit is contained in:
parent
4b31abfba2
commit
486d0d80c6
@ -33,6 +33,8 @@ import java.security.cert.X509Certificate;
|
||||
*/
|
||||
public class HttpClientUtils {
|
||||
|
||||
private static final boolean printLog = false;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class);
|
||||
|
||||
/**
|
||||
@ -79,8 +81,10 @@ public class HttpClientUtils {
|
||||
}
|
||||
|
||||
private static String doPostTextPlain(CloseableHttpClient httpClient, String url, String params) throws Exception {
|
||||
log.info("Post请求url:{}", url);
|
||||
log.info("Post请求params:{}", params);
|
||||
if (printLog) {
|
||||
log.info("Post请求url:{}", url);
|
||||
log.info("Post请求params:{}", params);
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
|
||||
StringEntity requestEntity = new StringEntity(params, "UTF-8");
|
||||
@ -110,7 +114,9 @@ public class HttpClientUtils {
|
||||
* 封装get请求方式的处理
|
||||
*/
|
||||
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);
|
||||
return execute(httpClient, httpGet);
|
||||
}
|
||||
@ -119,8 +125,10 @@ public class HttpClientUtils {
|
||||
* 封装post请求方式的处理
|
||||
*/
|
||||
private static String doPost(CloseableHttpClient httpClient, String url, String params) throws Exception {
|
||||
log.info("Post请求url:{}", url);
|
||||
log.info("Post请求params:{}", params);
|
||||
if (printLog) {
|
||||
log.info("Post请求url:{}", url);
|
||||
log.info("Post请求params:{}", params);
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
|
||||
httpPost.setEntity(new StringEntity(params));
|
||||
@ -136,15 +144,21 @@ public class HttpClientUtils {
|
||||
try {
|
||||
response = httpClient.execute(requestBase);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
log.info("HttpClient响应码={}", statusCode);
|
||||
if (printLog) {
|
||||
log.info("HttpClient响应码={}", statusCode);
|
||||
}
|
||||
final int success = 200;
|
||||
if (statusCode == success) {
|
||||
result = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||
} else {
|
||||
log.error("HttpClient请求失败,错误码={}", statusCode);
|
||||
if (printLog) {
|
||||
log.error("HttpClient请求失败,错误码={}", statusCode);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("HttpClient请求异常:", e);
|
||||
if (printLog) {
|
||||
log.error("HttpClient请求异常:", e);
|
||||
}
|
||||
} finally {
|
||||
if (null != httpClient) {
|
||||
httpClient.close();
|
||||
@ -153,7 +167,9 @@ public class HttpClientUtils {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
log.info("HttpClient请求结果:{}", result);
|
||||
if (printLog) {
|
||||
log.info("HttpClient请求结果:{}", result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user