|
@@ -1,21 +1,23 @@
|
|
|
package com.uas.ps.inquiry.util;
|
|
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.Consts;
|
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.NameValuePair;
|
|
import org.apache.http.NameValuePair;
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
-import org.apache.http.client.HttpClient;
|
|
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
import org.apache.http.client.methods.*;
|
|
import org.apache.http.client.methods.*;
|
|
|
|
|
+import org.apache.http.concurrent.FutureCallback;
|
|
|
import org.apache.http.entity.ContentType;
|
|
import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
import org.apache.http.entity.mime.content.FileBody;
|
|
import org.apache.http.entity.mime.content.FileBody;
|
|
|
import org.apache.http.entity.mime.content.StringBody;
|
|
import org.apache.http.entity.mime.content.StringBody;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
-import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
|
-import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
|
|
+import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.protocol.HTTP;
|
|
import org.apache.http.protocol.HTTP;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
@@ -44,6 +46,85 @@ import java.util.Set;
|
|
|
*/
|
|
*/
|
|
|
public class HttpUtil {
|
|
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请求
|
|
* 发送GET请求
|
|
|
*
|
|
*
|
|
@@ -175,14 +256,23 @@ public class HttpUtil {
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
public static String doPost(String postUrl, String formData) throws Exception {
|
|
public static String doPost(String postUrl, String formData) throws Exception {
|
|
|
- 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;
|
|
|
|
|
|
|
+ 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();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -251,21 +341,14 @@ public class HttpUtil {
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
public static Response sendHttpUriRequest(HttpRequestBase request) throws Exception {
|
|
public static Response sendHttpUriRequest(HttpRequestBase request) throws Exception {
|
|
|
- CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
|
|
|
|
+ CloseableHttpClient httpClient = clientSpringFactory.getHttpSyncClient();
|
|
|
CloseableHttpResponse response = null;
|
|
CloseableHttpResponse response = null;
|
|
|
try {
|
|
try {
|
|
|
response = httpClient.execute(request);
|
|
response = httpClient.execute(request);
|
|
|
return Response.getResponse(response);
|
|
return Response.getResponse(response);
|
|
|
} finally {
|
|
} finally {
|
|
|
- try {
|
|
|
|
|
- httpClient.close();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- }
|
|
|
|
|
- if (response != null) {
|
|
|
|
|
- try {
|
|
|
|
|
- response.close();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (request != null) {
|
|
|
|
|
+ request.releaseConnection();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -280,7 +363,7 @@ public class HttpUtil {
|
|
|
*/
|
|
*/
|
|
|
public static Response sendHttpEntityEnclosingRequest(HttpEntityEnclosingRequestBase request,
|
|
public static Response sendHttpEntityEnclosingRequest(HttpEntityEnclosingRequestBase request,
|
|
|
Map<String, Object> params, boolean encode) throws Exception {
|
|
Map<String, Object> params, boolean encode) throws Exception {
|
|
|
- CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
|
|
|
|
+ CloseableHttpClient httpClient = clientSpringFactory.getHttpSyncClient();
|
|
|
CloseableHttpResponse response = null;
|
|
CloseableHttpResponse response = null;
|
|
|
try {
|
|
try {
|
|
|
if (!encode) {
|
|
if (!encode) {
|
|
@@ -305,16 +388,8 @@ public class HttpUtil {
|
|
|
response = httpClient.execute(request);
|
|
response = httpClient.execute(request);
|
|
|
return Response.getResponse(response);
|
|
return Response.getResponse(response);
|
|
|
} finally {
|
|
} finally {
|
|
|
- request.releaseConnection();
|
|
|
|
|
- try {
|
|
|
|
|
- httpClient.close();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- }
|
|
|
|
|
- if (response != null) {
|
|
|
|
|
- try {
|
|
|
|
|
- response.close();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (request != null) {
|
|
|
|
|
+ request.releaseConnection();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -460,11 +535,13 @@ public class HttpUtil {
|
|
|
*/
|
|
*/
|
|
|
public static Response upload(String postUrl, String filePath, Map<String, String> params)
|
|
public static Response upload(String postUrl, String filePath, Map<String, String> params)
|
|
|
throws IllegalStateException, IOException, Exception {
|
|
throws IllegalStateException, IOException, Exception {
|
|
|
- CloseableHttpClient httpClient = null;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ CloseableHttpClient httpClient = clientSpringFactory.getHttpSyncClient();
|
|
|
CloseableHttpResponse response = null;
|
|
CloseableHttpResponse response = null;
|
|
|
|
|
+ HttpPost httpPost = null;
|
|
|
try {
|
|
try {
|
|
|
- httpClient = HttpClients.createDefault();
|
|
|
|
|
- HttpPost httpPost = new HttpPost(postUrl);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ httpPost = new HttpPost(postUrl);
|
|
|
FileBody fileBody = new FileBody(new File(filePath));
|
|
FileBody fileBody = new FileBody(new File(filePath));
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
builder.addPart("file", fileBody);
|
|
builder.addPart("file", fileBody);
|
|
@@ -479,20 +556,10 @@ public class HttpUtil {
|
|
|
httpPost.setEntity(reqEntity);
|
|
httpPost.setEntity(reqEntity);
|
|
|
response = httpClient.execute(httpPost);
|
|
response = httpClient.execute(httpPost);
|
|
|
} finally {
|
|
} finally {
|
|
|
- try {
|
|
|
|
|
- if (response != null) {
|
|
|
|
|
- response.close();
|
|
|
|
|
- }
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- try {
|
|
|
|
|
- if (httpClient != null) {
|
|
|
|
|
- httpClient.close();
|
|
|
|
|
- }
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
|
+ if (httpPost != null) {
|
|
|
|
|
+ httpPost.releaseConnection();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
return Response.getResponse(response);
|
|
return Response.getResponse(response);
|
|
|
}
|
|
}
|
|
@@ -506,10 +573,18 @@ public class HttpUtil {
|
|
|
* @throws IOException
|
|
* @throws IOException
|
|
|
*/
|
|
*/
|
|
|
public static InputStream download(String postUrl) throws ClientProtocolException, IOException {
|
|
public static InputStream download(String postUrl) throws ClientProtocolException, IOException {
|
|
|
- CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
|
|
- HttpGet httpGet = new HttpGet(postUrl);
|
|
|
|
|
- CloseableHttpResponse response = httpClient.execute(httpGet);
|
|
|
|
|
- return response.getEntity().getContent();
|
|
|
|
|
|
|
+ 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();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|