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
*/
public static String get(String url) throws Exception{
//实例化httpclient
CloseableHttpClient httpclient = HttpClients.createDefault();
//实例化get方法
HttpGet httpget = new HttpGet(url);
CloseableHttpClient httpclient =null;
//请求结果
CloseableHttpResponse response = null;
String content ="";
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 {
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) {
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content;
}
@@ -119,19 +122,20 @@ public class HttpClientUtil {
*/
public String post(String url,Map<String, String> params) throws ClientProtocolException, IOException{
//实例化httpClient
CloseableHttpClient 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)));
}
CloseableHttpClient httpclient =null;
//结果
CloseableHttpResponse response = null;
String content="";
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");
//将参数给post方法
@@ -179,18 +183,7 @@ public class HttpClientUtil {
* @throws ClientProtocolException
*/
public static String patch(String url,String param) throws Exception{
//实例化httpClient
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");
CloseableHttpClient httpclient = null;
//处理参数
// List<NameValuePair> nvps = new ArrayList <NameValuePair>();
@@ -202,27 +195,39 @@ public class HttpClientUtil {
CloseableHttpResponse response = null;
String content="";
try {
//提交的参数
// UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
StringEntity entity = new StringEntity(param, "utf-8");
//将参数给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();
//实例化httpClient
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");
//提交的参数
// UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
StringEntity entity = new StringEntity(param, "utf-8");
//将参数给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){
content = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally {
if (response != null) {
try {
@@ -267,7 +272,7 @@ public class HttpClientUtil {
url=uriBuilder.toString();
//创建连接
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);
response= header.get();