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