|
@@ -3,21 +3,19 @@ package com.huimv.production.autoGetData.utils;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import org.apache.http.*;
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
-import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
import org.apache.http.client.methods.*;
|
|
|
-import org.apache.http.conn.routing.HttpRoute;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
-import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
-import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.nio.charset.Charset;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@@ -29,40 +27,64 @@ import java.util.*;
|
|
|
* @Create : 2020-12-25
|
|
|
**/
|
|
|
@Component
|
|
|
-public class HttpClientUtil {
|
|
|
- String httpIp = "http://121.40.221.149:9100";
|
|
|
+public class HttpTemplete {
|
|
|
+ private String testHttpIp = "http://localhost:9105";
|
|
|
|
|
|
- public static void main(String[] args) throws Exception {
|
|
|
- //
|
|
|
- HttpClientUtil client = new HttpClientUtil();
|
|
|
-// client.testGetRequest1();
|
|
|
- client.testGetRequest2();
|
|
|
- }
|
|
|
+// public static void main(String[] args) throws Exception {
|
|
|
+// //
|
|
|
+// HttpTemplete client = new HttpTemplete();
|
|
|
+//// client.testGetRequest1();
|
|
|
+// client.testGetRequest2();
|
|
|
+// }
|
|
|
|
|
|
- private void testGetRequest1() throws IOException {
|
|
|
- Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
|
|
|
- timeoutMap.put("connectTimeout", 5000);
|
|
|
- timeoutMap.put("requestTimeout", 5000);
|
|
|
- timeoutMap.put("socketTimeout", 5000);
|
|
|
- String url = httpIp + "/token/getToken?userId=20210501×tamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c";
|
|
|
- // test error
|
|
|
- url = httpIp + "/token/getToken?userId=20210501×tamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c&userId=20210501×tamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c";
|
|
|
+ public JSONObject post(String url, Map<String, String> paramsMap, Map<String, Integer> timeoutMap) throws IOException {
|
|
|
+ // 创建默认的httpClient实例.
|
|
|
CloseableHttpClient httpClient = getHttpClientConnection();
|
|
|
- CloseableHttpResponse response = httpClient.execute(getHttpRequest(url, timeoutMap));
|
|
|
- // 打印响应状态
|
|
|
- System.out.println("响应状态1 =" + response.getStatusLine());
|
|
|
+ //
|
|
|
+ int connectTimeout = timeoutMap.get("connectTimeout");
|
|
|
+ int requestTimeout = timeoutMap.get("requestTimeout");
|
|
|
+ int socketTimeout = timeoutMap.get("socketTimeout");
|
|
|
+ // 创建httpPost
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ .setSocketTimeout(socketTimeout) //服务器响应超时时间
|
|
|
+ .setConnectTimeout(connectTimeout) //连接服务器超时时间
|
|
|
+ .setConnectionRequestTimeout(requestTimeout)
|
|
|
+ .build();
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+ // 创建参数队列
|
|
|
+ List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
|
|
|
+ Set<Map.Entry<String, String>> entrySet = paramsMap.entrySet();
|
|
|
+ for (Map.Entry<String, String> e : entrySet) {
|
|
|
+ String name = e.getKey();
|
|
|
+ String value = e.getValue();
|
|
|
+ NameValuePair pair = new BasicNameValuePair(name, value);
|
|
|
+ paramsList.add(pair);
|
|
|
+ }
|
|
|
+ UrlEncodedFormEntity content = new UrlEncodedFormEntity(paramsList, "UTF-8");
|
|
|
+ //处理乱码
|
|
|
+// StringEntity content = new StringEntity(paramsList.toString(), Charset.forName("UTF-8"));// 第二个参数,设置后才会对,内容进行编码
|
|
|
+// content.setContentType("application/soap+xml; charset=UTF-8");
|
|
|
+// content.setContentEncoding("UTF-8");
|
|
|
+ httpPost.setEntity(content);
|
|
|
+// HttpUriRequest httpPost = RequestBuilder.post().setUri(url).setEntity(content).setConfig(requestConfig);
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
|
+ // 获取响应实体
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
try {
|
|
|
if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
- // 获取响应实体
|
|
|
- HttpEntity entity = response.getEntity();
|
|
|
// 打印响应内容
|
|
|
String text = EntityUtils.toString(entity, "utf-8");
|
|
|
- System.out.println("响应内容=" + text);
|
|
|
-
|
|
|
- JSONObject resultJo = JSONObject.parseObject(text);
|
|
|
- System.out.println("accessToken="+resultJo.getString("accessToken"));
|
|
|
- } else {
|
|
|
- System.out.println("请求失败");
|
|
|
+ JSONObject resultJo = new JSONObject();
|
|
|
+ resultJo.put("status", true);
|
|
|
+ resultJo.put("content", text);
|
|
|
+ return resultJo;
|
|
|
+ }else{
|
|
|
+ String text = EntityUtils.toString(entity, "utf-8");
|
|
|
+ JSONObject resultJo = new JSONObject();
|
|
|
+ resultJo.put("status", false);
|
|
|
+ resultJo.put("content", text);
|
|
|
+ return resultJo;
|
|
|
}
|
|
|
} finally {
|
|
|
response.close();
|
|
@@ -70,76 +92,90 @@ public class HttpClientUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void testGetRequest2() throws IOException {
|
|
|
- Map<String, String> paramsMap = new HashMap<String, String>();
|
|
|
- paramsMap.put("userId", "20210501");
|
|
|
- paramsMap.put("timestamp", "45546546454");
|
|
|
- paramsMap.put("random", "1156");
|
|
|
- paramsMap.put("sign", "7fa431325504e01e9fa87ed0e274c40c");
|
|
|
-
|
|
|
- Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
|
|
|
- timeoutMap.put("connectTimeout", 5000);
|
|
|
- timeoutMap.put("requestTimeout", 5000);
|
|
|
- timeoutMap.put("socketTimeout", 5000);
|
|
|
-
|
|
|
- String url = httpIp + "/token/getToken";
|
|
|
- // get(url, paramsMap, timeoutMap);
|
|
|
- //
|
|
|
- JSONObject resultJo = get(url,paramsMap,timeoutMap);
|
|
|
- System.out.println("status="+resultJo.getBoolean("status"));
|
|
|
- System.out.println("content="+resultJo.getJSONObject("content"));
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
- * @Method : get
|
|
|
- * @Description :
|
|
|
- * @Params : [url, paramsMap, timeoutMap]
|
|
|
- * @Return : com.alibaba.fastjson.JSONObject
|
|
|
- *
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Date : 2021/11/18
|
|
|
- * @Time : 17:39
|
|
|
+ * @Method : get
|
|
|
+ * @Description :
|
|
|
+ * @Params : [url, paramsMap, timeoutMap]
|
|
|
+ * @Return : com.alibaba.fastjson.JSONObject
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2021/11/18
|
|
|
+ * @Time : 17:39
|
|
|
*/
|
|
|
public JSONObject get(String url, Map<String, String> paramsMap, Map<String, Integer> timeoutMap) throws IOException {
|
|
|
- if(timeoutMap == null){
|
|
|
+ if (timeoutMap == null) {
|
|
|
timeoutMap.put("connectTimeout", 5000);
|
|
|
timeoutMap.put("requestTimeout", 5000);
|
|
|
timeoutMap.put("socketTimeout", 5000);
|
|
|
}
|
|
|
//
|
|
|
CloseableHttpClient httpClient = getHttpClientConnection();
|
|
|
- //
|
|
|
+ //执行//获取请求内容
|
|
|
CloseableHttpResponse response = httpClient.execute(getHttpRequest(url, paramsMap, timeoutMap));
|
|
|
try {
|
|
|
+ // 获取响应实体
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
// 打印响应状态
|
|
|
if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
- // 获取响应实体
|
|
|
- HttpEntity entity = response.getEntity();
|
|
|
// 打印响应内容
|
|
|
String text = EntityUtils.toString(entity, "utf-8");
|
|
|
JSONObject resultJo = new JSONObject();
|
|
|
- resultJo.put("status",true);
|
|
|
- resultJo.put("content",text);
|
|
|
+ resultJo.put("status", true);
|
|
|
+ resultJo.put("content", text);
|
|
|
return resultJo;
|
|
|
} else {
|
|
|
+ String text = EntityUtils.toString(entity, "utf-8");
|
|
|
JSONObject resultJo = new JSONObject();
|
|
|
- resultJo.put("status",false);
|
|
|
- resultJo.put("content","请求失败");
|
|
|
+ resultJo.put("status", false);
|
|
|
+ resultJo.put("content", text);
|
|
|
return resultJo;
|
|
|
}
|
|
|
} finally {
|
|
|
response.close();
|
|
|
httpClient.close();
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ private void testGetRequest1() throws IOException {
|
|
|
+ Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
|
|
|
+ timeoutMap.put("connectTimeout", 5000);
|
|
|
+ timeoutMap.put("requestTimeout", 5000);
|
|
|
+ timeoutMap.put("socketTimeout", 5000);
|
|
|
+ String url = testHttpIp + "/token/getToken?userId=20210501×tamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c";
|
|
|
+ // test error
|
|
|
+ url = testHttpIp + "/token/getToken?userId=20210501×tamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c&userId=20210501×tamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c";
|
|
|
+ CloseableHttpClient httpClient = getHttpClientConnection();
|
|
|
+ CloseableHttpResponse response = httpClient.execute(getHttpRequest(url, timeoutMap));
|
|
|
+ // 打印响应状态
|
|
|
+ System.out.println("响应状态1 =" + response.getStatusLine());
|
|
|
+ try {
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ // 获取响应实体
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ // 打印响应内容
|
|
|
+ String text = EntityUtils.toString(entity, "utf-8");
|
|
|
+ System.out.println("响应内容=" + text);
|
|
|
|
|
|
+ JSONObject resultJo = JSONObject.parseObject(text);
|
|
|
+ System.out.println("accessToken=" + resultJo.getString("accessToken"));
|
|
|
+ } else {
|
|
|
+ System.out.println("请求失败");
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ response.close();
|
|
|
+ httpClient.close();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ //
|
|
|
private CloseableHttpClient getHttpClientConnection() {
|
|
|
+ //创建默认实例
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
+ //
|
|
|
// CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
return httpclient;
|
|
|
}
|
|
|
-
|
|
|
+ //
|
|
|
private HttpUriRequest getHttpRequest(String url, Map<String, String> map, Map<String, Integer> timeoutMap) {
|
|
|
int connectTimeout = timeoutMap.get("connectTimeout");
|
|
|
int requestTimeout = timeoutMap.get("requestTimeout");
|
|
@@ -153,6 +189,7 @@ public class HttpClientUtil {
|
|
|
NameValuePair pair = new BasicNameValuePair(name, value);
|
|
|
params.add(pair);
|
|
|
}
|
|
|
+ //
|
|
|
RequestConfig requestConfig = RequestConfig.custom()
|
|
|
.setConnectTimeout(connectTimeout).setConnectionRequestTimeout(requestTimeout)
|
|
|
.setSocketTimeout(socketTimeout).build();
|
|
@@ -161,7 +198,7 @@ public class HttpClientUtil {
|
|
|
.setConfig(requestConfig).build();
|
|
|
return httpGet;
|
|
|
}
|
|
|
-
|
|
|
+ //
|
|
|
private HttpGet getHttpRequest(String url, Map<String, Integer> timeoutMap) {
|
|
|
int connectTimeout = timeoutMap.get("connectTimeout");
|
|
|
int requestTimeout = timeoutMap.get("requestTimeout");
|
|
@@ -177,7 +214,7 @@ public class HttpClientUtil {
|
|
|
}
|
|
|
|
|
|
//test
|
|
|
- public void get() {
|
|
|
+ private void get() {
|
|
|
// CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
// try {
|
|
|
// // 创建httpget.
|