|
@@ -1,7 +1,6 @@
|
|
|
package com.skyversation.xjcy.util;
|
|
package com.skyversation.xjcy.util;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.client.HttpClient;
|
|
import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
@@ -16,7 +15,6 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
@@ -38,11 +36,7 @@ public class HttpUtil {
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String requestPost(String url, MultiValueMap<String, Object> params, Map<String, String> headerMap) {
|
|
public static String requestPost(String url, MultiValueMap<String, Object> params, Map<String, String> headerMap) {
|
|
|
- SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
|
|
|
- requestFactory.setConnectTimeout(10 * 1000);
|
|
|
|
|
- requestFactory.setReadTimeout(10 * 1000);
|
|
|
|
|
- RestTemplate client = new RestTemplate(requestFactory);
|
|
|
|
|
- client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
|
|
|
|
|
|
+ RestTemplate client = getRestTemplate();
|
|
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
@@ -60,32 +54,76 @@ public class HttpUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static String requestGet(String url,
|
|
public static String requestGet(String url,
|
|
|
- MultiValueMap<String, String> params,
|
|
|
|
|
- Map<String, String> headerMap) {
|
|
|
|
|
|
|
+ MultiValueMap<String, String> params,
|
|
|
|
|
+ Map<String, String> headerMap) {
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<String> res = requestWithRes(url, params,null, headerMap, String.class, HttpMethod.GET);
|
|
|
|
|
+ return res.getBody();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static <T> ResponseEntity<T> requestWithRes(String url,
|
|
|
|
|
+ MultiValueMap<String, String> params,
|
|
|
|
|
+ MultiValueMap<String, String> body,
|
|
|
|
|
+ Map<String, String> headerMap,
|
|
|
|
|
+ Class<T> clazz,
|
|
|
|
|
+ HttpMethod method) {
|
|
|
|
|
+ RestTemplate client = getRestTemplate();
|
|
|
|
|
+
|
|
|
|
|
+ // 构建带参数的完整 URL
|
|
|
|
|
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
|
|
|
|
+ if (params != null) {
|
|
|
|
|
+ builder.queryParams(params);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 构建 headers
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ if (headerMap != null) {
|
|
|
|
|
+ headerMap.forEach(headers::add);
|
|
|
|
|
+ }
|
|
|
|
|
+ HttpEntity<?> entity;
|
|
|
|
|
+ if (body != null) {
|
|
|
|
|
+ entity = new HttpEntity<>(body, headers);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ entity = new HttpEntity<>(headers);
|
|
|
|
|
+ }
|
|
|
|
|
+ URI uri = URI.create(builder.toUriString());
|
|
|
|
|
+ return client.exchange(uri, method, entity, clazz);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static RestTemplate getRestTemplate() {
|
|
|
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
|
requestFactory.setConnectTimeout(10 * 1000);
|
|
requestFactory.setConnectTimeout(10 * 1000);
|
|
|
requestFactory.setReadTimeout(10 * 1000);
|
|
requestFactory.setReadTimeout(10 * 1000);
|
|
|
RestTemplate client = new RestTemplate(requestFactory);
|
|
RestTemplate client = new RestTemplate(requestFactory);
|
|
|
client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
|
client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
|
|
|
|
+ return client;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static ResponseEntity<byte[]> requestPostWithJson(String url,
|
|
|
|
|
+ Map<String, String> body,
|
|
|
|
|
+ Map<String, String> headerMap){
|
|
|
|
|
+ RestTemplate client = getRestTemplate();
|
|
|
|
|
|
|
|
// 构建带参数的完整 URL
|
|
// 构建带参数的完整 URL
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
|
|
- if (params != null) {
|
|
|
|
|
- builder.queryParams(params);
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
// 构建 headers
|
|
// 构建 headers
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
if (headerMap != null) {
|
|
if (headerMap != null) {
|
|
|
headerMap.forEach(headers::add);
|
|
headerMap.forEach(headers::add);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- HttpEntity<?> entity = new HttpEntity<>(headers);
|
|
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+ HttpEntity<?> entity;
|
|
|
|
|
+ if (body != null) {
|
|
|
|
|
+ String json = JSON.toJSONString(body);
|
|
|
|
|
+ entity = new HttpEntity<>(json, headers);
|
|
|
|
|
+ }else {
|
|
|
|
|
+ entity = new HttpEntity<>(headers);
|
|
|
|
|
+ }
|
|
|
URI uri = URI.create(builder.toUriString());
|
|
URI uri = URI.create(builder.toUriString());
|
|
|
- return client.exchange(uri, HttpMethod.GET, entity, String.class).getBody();
|
|
|
|
|
|
|
+ return client.exchange(uri, HttpMethod.POST, entity, byte[].class);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
public static ResponseEntity<String> requestGetHttps(String url, MultiValueMap<String, String> params, Map<String, String> headerMap) {
|
|
public static ResponseEntity<String> requestGetHttps(String url, MultiValueMap<String, String> params, Map<String, String> headerMap) {
|
|
|
try {
|
|
try {
|
|
|
// 创建忽略 SSL 验证的 HttpClient
|
|
// 创建忽略 SSL 验证的 HttpClient
|
|
@@ -127,8 +165,6 @@ public class HttpUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
public static String sendGetRequest(String urlString) throws Exception {
|
|
public static String sendGetRequest(String urlString) throws Exception {
|
|
|
HttpClient httpClient = HttpClients.createDefault();
|
|
HttpClient httpClient = HttpClients.createDefault();
|
|
|
HttpGet httpGet = new HttpGet(urlString);
|
|
HttpGet httpGet = new HttpGet(urlString);
|