httpclientutil所有处理放在try中

This commit is contained in:
DuanDongmei
2018-11-22 11:32:52 +08:00
parent 4c57ecffd2
commit feb52af4e9

View File

@@ -65,47 +65,50 @@ public class HttpClientUtil {
* @throws ClientProtocolException * @throws ClientProtocolException
*/ */
public static String get(String url) throws Exception{ public static String get(String url) throws Exception{
//实例化httpclient
CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient =null;
//实例化get方法
HttpGet httpget = new HttpGet(url);
//请求结果 //请求结果
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
String content =""; String content ="";
logger.info("流量统计数据请求路径:"+url); logger.info("流量统计数据请求路径:"+url);
//执行get方法 //执行get方法
try {
//实例化get方法
HttpGet httpget = new HttpGet(url);
//实例化httpclient
httpclient = HttpClients.createDefault();
requestConfig = RequestConfig.custom()
.setSocketTimeout( Constants.HTTP_SOCKET_TIMEOUT)
.setConnectTimeout( Constants.HTTP_CONNECT_TIMEOUT)
.setConnectionRequestTimeout( Constants.HTTP_CONNECT_REQUEST_TIMEOUT)
.build();
httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setRetryHandler(new DefaultHttpRequestRetryHandler( Constants.HTTP_CONNECT_RETRY_TIMES, false))
.build();
response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
content = EntityUtils.toString(response.getEntity(),"utf-8");
logger.info("获取流量统计数据成功,相应内容如下: " + content);
}else {
logger.error("获取消息失败,相应内容如下: " + content);
throw new ConnectException("流量统计服务接口连接错误"+content);
}
} catch (Exception e) {
throw e;
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try { try {
requestConfig = RequestConfig.custom() httpclient.close();
.setSocketTimeout( Constants.HTTP_SOCKET_TIMEOUT) } catch (IOException e) {
.setConnectTimeout( Constants.HTTP_CONNECT_TIMEOUT) e.printStackTrace();
.setConnectionRequestTimeout( Constants.HTTP_CONNECT_REQUEST_TIMEOUT) }
.build(); }
httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setRetryHandler(new DefaultHttpRequestRetryHandler( Constants.HTTP_CONNECT_RETRY_TIMES, false))
.build();
response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
content = EntityUtils.toString(response.getEntity(),"utf-8");
logger.info("获取流量统计数据成功,相应内容如下: " + content);
}else {
logger.error("获取消息失败,相应内容如下: " + content);
throw new ConnectException("流量统计服务接口连接错误"+content);
}
} catch (Exception e) {
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content; return content;
} }
@@ -119,19 +122,20 @@ public class HttpClientUtil {
*/ */
public String post(String url,Map<String, String> params) throws ClientProtocolException, IOException{ public String post(String url,Map<String, String> params) throws ClientProtocolException, IOException{
//实例化httpClient //实例化httpClient
CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient =null;
//实例化post方法
HttpPost httpPost = new HttpPost(url);
//处理参数
List<NameValuePair> nvps = new ArrayList <NameValuePair>();
Set<String> keySet = params.keySet();
for(String key : keySet) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
//结果 //结果
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
String content=""; String content="";
try { try {
httpclient = HttpClients.createDefault();
//实例化post方法
HttpPost httpPost = new HttpPost(url);
//处理参数
List<NameValuePair> nvps = new ArrayList <NameValuePair>();
Set<String> keySet = params.keySet();
for(String key : keySet) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
//提交的参数 //提交的参数
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8"); UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
//将参数给post方法 //将参数给post方法
@@ -179,18 +183,7 @@ public class HttpClientUtil {
* @throws ClientProtocolException * @throws ClientProtocolException
*/ */
public static String patch(String url,String param) throws Exception{ public static String patch(String url,String param) throws Exception{
//实例化httpClient CloseableHttpClient httpclient = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
//实例化patch方法
HttpPatch httpPatch = new HttpPatch(url);
httpPatch.setHeader("Content-type", "application/json");
httpPatch.setHeader("Charset", "utf-8");
httpPatch.setHeader("Accept", "application/json");
httpPatch.setHeader("Accept-Charset", "utf-8");
//处理参数 //处理参数
// List<NameValuePair> nvps = new ArrayList <NameValuePair>(); // List<NameValuePair> nvps = new ArrayList <NameValuePair>();
@@ -202,27 +195,39 @@ public class HttpClientUtil {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
String content=""; String content="";
try { try {
//提交的参数 //实例化httpClient
// UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8"); httpclient = HttpClients.createDefault();
StringEntity entity = new StringEntity(param, "utf-8"); //实例化patch方法
//将参数给post方法 HttpPatch httpPatch = new HttpPatch(url);
httpPatch.setEntity(entity);
requestConfig = RequestConfig.custom() httpPatch.setHeader("Content-type", "application/json");
.setSocketTimeout( Constants.HTTP_SOCKET_TIMEOUT)
.setConnectTimeout( Constants.HTTP_CONNECT_TIMEOUT) httpPatch.setHeader("Charset", "utf-8");
.setConnectionRequestTimeout( Constants.HTTP_CONNECT_REQUEST_TIMEOUT)
.build(); httpPatch.setHeader("Accept", "application/json");
httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setRetryHandler(new DefaultHttpRequestRetryHandler( Constants.HTTP_CONNECT_RETRY_TIMES, false)) httpPatch.setHeader("Accept-Charset", "utf-8");
.build(); //提交的参数
//执行post方法 // UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
response = httpclient.execute(httpPatch); StringEntity entity = new StringEntity(param, "utf-8");
int status = response.getStatusLine().getStatusCode(); //将参数给post方法
httpPatch.setEntity(entity);
requestConfig = RequestConfig.custom()
.setSocketTimeout( Constants.HTTP_SOCKET_TIMEOUT)
.setConnectTimeout( Constants.HTTP_CONNECT_TIMEOUT)
.setConnectionRequestTimeout( Constants.HTTP_CONNECT_REQUEST_TIMEOUT)
.build();
httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setRetryHandler(new DefaultHttpRequestRetryHandler( Constants.HTTP_CONNECT_RETRY_TIMES, false))
.build();
//执行post方法
response = httpclient.execute(httpPatch);
int status = response.getStatusLine().getStatusCode();
if(status==200){ if(status==200){
content = EntityUtils.toString(response.getEntity()); content = EntityUtils.toString(response.getEntity());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); throw e;
}finally { }finally {
if (response != null) { if (response != null) {
try { try {
@@ -267,7 +272,7 @@ public class HttpClientUtil {
url=uriBuilder.toString(); url=uriBuilder.toString();
//创建连接 //创建连接
WebTarget wt = ClientUtil.getWebTarger(url); WebTarget wt = ClientUtil.getWebTarger(url);
logger.info("getId url:"+url); logger.info("getMsg url:"+url);
//获取响应结果 //获取响应结果
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON); Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
response= header.get(); response= header.get();