httpclientutil所有处理放在try中

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

View File

@@ -65,16 +65,18 @@ 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 { try {
//实例化get方法
HttpGet httpget = new HttpGet(url);
//实例化httpclient
httpclient = HttpClients.createDefault();
requestConfig = RequestConfig.custom() requestConfig = RequestConfig.custom()
.setSocketTimeout( Constants.HTTP_SOCKET_TIMEOUT) .setSocketTimeout( Constants.HTTP_SOCKET_TIMEOUT)
.setConnectTimeout( Constants.HTTP_CONNECT_TIMEOUT) .setConnectTimeout( Constants.HTTP_CONNECT_TIMEOUT)
@@ -92,6 +94,7 @@ public class HttpClientUtil {
throw new ConnectException("流量统计服务接口连接错误"+content); throw new ConnectException("流量统计服务接口连接错误"+content);
} }
} catch (Exception e) { } catch (Exception e) {
throw e;
}finally { }finally {
if (response != null) { if (response != null) {
try { try {
@@ -119,7 +122,12 @@ 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;
//结果
CloseableHttpResponse response = null;
String content="";
try {
httpclient = HttpClients.createDefault();
//实例化post方法 //实例化post方法
HttpPost httpPost = new HttpPost(url); HttpPost httpPost = new HttpPost(url);
//处理参数 //处理参数
@@ -128,10 +136,6 @@ public class HttpClientUtil {
for(String key : keySet) { for(String key : keySet) {
nvps.add(new BasicNameValuePair(key, params.get(key))); nvps.add(new BasicNameValuePair(key, params.get(key)));
} }
//结果
CloseableHttpResponse response = null;
String content="";
try {
//提交的参数 //提交的参数
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,6 +195,18 @@ public class HttpClientUtil {
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
String content=""; String content="";
try { try {
//实例化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"); // UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
StringEntity entity = new StringEntity(param, "utf-8"); StringEntity entity = new StringEntity(param, "utf-8");
@@ -222,7 +227,7 @@ public class HttpClientUtil {
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();