|
|
@@ -1,23 +1,21 @@
|
|
|
package com.uas.ps.inquiry.util;
|
|
|
|
|
|
-import com.uas.ps.core.util.ContextUtils;
|
|
|
-import com.uas.ps.inquiry.http.HttpAsyncCallback;
|
|
|
-import com.uas.ps.inquiry.http.HttpClientSpringFactory;
|
|
|
import org.apache.http.Consts;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.NameValuePair;
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
import org.apache.http.client.methods.*;
|
|
|
-import org.apache.http.concurrent.FutureCallback;
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
import org.apache.http.entity.mime.content.FileBody;
|
|
|
import org.apache.http.entity.mime.content.StringBody;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
-import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
|
|
+import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.protocol.HTTP;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
@@ -40,94 +38,15 @@ import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* HTTP工具类,封装http请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @author suntg
|
|
|
* @date 2015年3月5日14:20:40
|
|
|
*/
|
|
|
public class HttpUtil {
|
|
|
|
|
|
- private static HttpClientSpringFactory clientSpringFactory;
|
|
|
-
|
|
|
- private static HttpAsyncCallback httpAsyncCallback;
|
|
|
-
|
|
|
- static {
|
|
|
- clientSpringFactory = ContextUtils.getBean(HttpClientSpringFactory.class);
|
|
|
- httpAsyncCallback = ContextUtils.getBean(HttpAsyncCallback.class);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 异步 get 请求,使用默认callback
|
|
|
- * @param url
|
|
|
- * @param params
|
|
|
- */
|
|
|
- public static void doGetAsync(String url, Map<String, ?> params) {
|
|
|
- doGetAsync(url, params, false, httpAsyncCallback);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 异步 get 请求,使用自定义callback
|
|
|
- * @param url
|
|
|
- * @param params
|
|
|
- */
|
|
|
- public static void doGetAsync(String url, Map<String, ?> params, FutureCallback<HttpResponse> callback) {
|
|
|
- doGetAsync(url, params, false, callback);
|
|
|
- }
|
|
|
-
|
|
|
- public static void doGetAsync(String url, Map<String, ?> params, boolean sign, FutureCallback<HttpResponse> callback) {
|
|
|
- HttpGet httpGet = null;
|
|
|
- try {
|
|
|
- httpGet = new HttpGet(getRequestUrl(url, (Map<String, Object>) params, sign));
|
|
|
- CloseableHttpAsyncClient httpAsyncClient = clientSpringFactory.getHttpAsyncClient();
|
|
|
- httpAsyncClient.start();
|
|
|
- httpAsyncClient.execute(httpGet, callback);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- if (httpGet != null) {
|
|
|
- httpGet.releaseConnection();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 异步 post 请求,默认使用 HttpAsyncCallback 回调
|
|
|
- * @param postUrl
|
|
|
- * @param formData
|
|
|
- */
|
|
|
- public static void doPostAsync(String postUrl, String formData) {
|
|
|
-
|
|
|
- doPostAsync(postUrl, formData, httpAsyncCallback);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 异步 post 请求,自定义回调
|
|
|
- * @param postUrl
|
|
|
- * @param formData
|
|
|
- */
|
|
|
- public static void doPostAsync(String postUrl, String formData, FutureCallback<HttpResponse> callback) {
|
|
|
-
|
|
|
- HttpPost post = null;
|
|
|
- try {
|
|
|
- CloseableHttpAsyncClient httpAsyncClient = clientSpringFactory.getHttpAsyncClient();
|
|
|
- httpAsyncClient.start();
|
|
|
- post = new HttpPost(postUrl);
|
|
|
- StringEntity postingString = new StringEntity(formData, HTTP.UTF_8);
|
|
|
- post.setEntity(postingString);
|
|
|
- post.setHeader("Content-type", "application/json");
|
|
|
- httpAsyncClient.execute(post, callback);
|
|
|
-
|
|
|
- } finally {
|
|
|
- if (post != null) {
|
|
|
- post.releaseConnection();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 发送GET请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @return
|
|
|
@@ -139,7 +58,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 发送GET请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @param sign
|
|
|
@@ -204,7 +123,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 发送POST请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @return
|
|
|
@@ -216,7 +135,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 发送POST请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @param sign
|
|
|
@@ -231,7 +150,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 发送POST请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @param sign
|
|
|
@@ -256,31 +175,22 @@ public class HttpUtil {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static String doPost(String postUrl, String formData) throws Exception {
|
|
|
- HttpPost post = null;
|
|
|
- try {
|
|
|
- CloseableHttpClient httpClient = clientSpringFactory.getHttpSyncClient();
|
|
|
- post = new HttpPost(postUrl);
|
|
|
- StringEntity postingString = new StringEntity(formData, HTTP.UTF_8);
|
|
|
- post.setEntity(postingString);
|
|
|
- post.setHeader("Content-type", "application/json");
|
|
|
- HttpResponse response = httpClient.execute(post);
|
|
|
- String content = EntityUtils.toString(response.getEntity());
|
|
|
- return content;
|
|
|
- } finally {
|
|
|
- if (post != null) {
|
|
|
- post.releaseConnection();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+ HttpClient httpClient = new DefaultHttpClient();
|
|
|
+ HttpPost post = new HttpPost(postUrl);
|
|
|
+ StringEntity postingString = new StringEntity(formData, HTTP.UTF_8);
|
|
|
+ post.setEntity(postingString);
|
|
|
+ post.setHeader("Content-type", "application/json");
|
|
|
+ HttpResponse response = httpClient.execute(post);
|
|
|
+ String content = EntityUtils.toString(response.getEntity());
|
|
|
+ return content;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送DELETE请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
- *
|
|
|
+ *
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@@ -290,7 +200,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 发送DELETE请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @param sign
|
|
|
@@ -318,52 +228,59 @@ public class HttpUtil {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static Response sendRequest(RequestMethod method, String url, Map<String, Object> params, boolean sign,
|
|
|
- boolean encode) throws Exception {
|
|
|
+ boolean encode) throws Exception {
|
|
|
switch (method) {
|
|
|
- case GET:
|
|
|
- return sendHttpUriRequest(new HttpGet(getRequestUrl(url, params, sign)));
|
|
|
- case POST:
|
|
|
- return sendHttpEntityEnclosingRequest(new HttpPost(getRequestUrl(url, sign)), params, encode);
|
|
|
- case PUT:
|
|
|
- return sendHttpEntityEnclosingRequest(new HttpPut(getRequestUrl(url, sign)), params, encode);
|
|
|
- case DELETE:
|
|
|
- return sendHttpUriRequest(new HttpDelete(getRequestUrl(url, params, sign)));
|
|
|
- default:
|
|
|
- return sendHttpUriRequest(new HttpGet(getRequestUrl(url, params, sign)));
|
|
|
+ case GET:
|
|
|
+ return sendHttpUriRequest(new HttpGet(getRequestUrl(url, params, sign)));
|
|
|
+ case POST:
|
|
|
+ return sendHttpEntityEnclosingRequest(new HttpPost(getRequestUrl(url, sign)), params, encode);
|
|
|
+ case PUT:
|
|
|
+ return sendHttpEntityEnclosingRequest(new HttpPut(getRequestUrl(url, sign)), params, encode);
|
|
|
+ case DELETE:
|
|
|
+ return sendHttpUriRequest(new HttpDelete(getRequestUrl(url, params, sign)));
|
|
|
+ default:
|
|
|
+ return sendHttpUriRequest(new HttpGet(getRequestUrl(url, params, sign)));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发起GET、DELETE请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static Response sendHttpUriRequest(HttpRequestBase request) throws Exception {
|
|
|
- CloseableHttpClient httpClient = clientSpringFactory.getHttpSyncClient();
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
CloseableHttpResponse response = null;
|
|
|
try {
|
|
|
response = httpClient.execute(request);
|
|
|
return Response.getResponse(response);
|
|
|
} finally {
|
|
|
- if (request != null) {
|
|
|
- request.releaseConnection();
|
|
|
+ try {
|
|
|
+ httpClient.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发起POST、PUT请求
|
|
|
- *
|
|
|
+ *
|
|
|
* @param request
|
|
|
* @param params
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static Response sendHttpEntityEnclosingRequest(HttpEntityEnclosingRequestBase request,
|
|
|
- Map<String, Object> params, boolean encode) throws Exception {
|
|
|
- CloseableHttpClient httpClient = clientSpringFactory.getHttpSyncClient();
|
|
|
+ Map<String, Object> params, boolean encode) throws Exception {
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
CloseableHttpResponse response = null;
|
|
|
try {
|
|
|
if (!encode) {
|
|
|
@@ -388,8 +305,16 @@ public class HttpUtil {
|
|
|
response = httpClient.execute(request);
|
|
|
return Response.getResponse(response);
|
|
|
} finally {
|
|
|
- if (request != null) {
|
|
|
- request.releaseConnection();
|
|
|
+ request.releaseConnection();
|
|
|
+ try {
|
|
|
+ httpClient.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -448,7 +373,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 将输入流转为字节数组
|
|
|
- *
|
|
|
+ *
|
|
|
* @param inStream
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
@@ -467,7 +392,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 将输入流转为字符串
|
|
|
- *
|
|
|
+ *
|
|
|
* @param inStream
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
@@ -490,7 +415,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* 发送xml数据
|
|
|
- *
|
|
|
+ *
|
|
|
* @param path
|
|
|
* 请求地址
|
|
|
* @param xml
|
|
|
@@ -521,7 +446,7 @@ public class HttpUtil {
|
|
|
|
|
|
/**
|
|
|
* http上传文件
|
|
|
- *
|
|
|
+ *
|
|
|
* @param postUrl
|
|
|
* 请求地址
|
|
|
* @param filePath
|
|
|
@@ -535,13 +460,11 @@ public class HttpUtil {
|
|
|
*/
|
|
|
public static Response upload(String postUrl, String filePath, Map<String, String> params)
|
|
|
throws IllegalStateException, IOException, Exception {
|
|
|
-
|
|
|
- CloseableHttpClient httpClient = clientSpringFactory.getHttpSyncClient();
|
|
|
+ CloseableHttpClient httpClient = null;
|
|
|
CloseableHttpResponse response = null;
|
|
|
- HttpPost httpPost = null;
|
|
|
try {
|
|
|
-
|
|
|
- httpPost = new HttpPost(postUrl);
|
|
|
+ httpClient = HttpClients.createDefault();
|
|
|
+ HttpPost httpPost = new HttpPost(postUrl);
|
|
|
FileBody fileBody = new FileBody(new File(filePath));
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
builder.addPart("file", fileBody);
|
|
|
@@ -556,40 +479,42 @@ public class HttpUtil {
|
|
|
httpPost.setEntity(reqEntity);
|
|
|
response = httpClient.execute(httpPost);
|
|
|
} finally {
|
|
|
- if (httpPost != null) {
|
|
|
- httpPost.releaseConnection();
|
|
|
+ try {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (httpClient != null) {
|
|
|
+ httpClient.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
return Response.getResponse(response);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 下载
|
|
|
- *
|
|
|
+ *
|
|
|
* @param postUrl
|
|
|
* @return
|
|
|
* @throws ClientProtocolException
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static InputStream download(String postUrl) throws ClientProtocolException, IOException {
|
|
|
- HttpGet httpGet = null;
|
|
|
- try {
|
|
|
- CloseableHttpClient httpClient = clientSpringFactory.getHttpSyncClient();
|
|
|
- httpGet = new HttpGet(postUrl);
|
|
|
- CloseableHttpResponse response = httpClient.execute(httpGet);
|
|
|
- return response.getEntity().getContent();
|
|
|
- } finally {
|
|
|
- if (httpGet != null) {
|
|
|
- httpGet.releaseConnection();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpGet httpGet = new HttpGet(postUrl);
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpGet);
|
|
|
+ return response.getEntity().getContent();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取请求客户端的IP地址
|
|
|
- *
|
|
|
+ *
|
|
|
* @param request
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
@@ -613,10 +538,10 @@ public class HttpUtil {
|
|
|
}
|
|
|
return ip;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* POST发送
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param data
|
|
|
* @throws Exception
|