|
|
|
|
@ -10,10 +10,7 @@ import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
|
|
import org.apache.http.ssl.SSLContextBuilder;
|
|
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.http.*;
|
|
|
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
|
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
@ -28,6 +25,36 @@ import java.security.KeyStoreException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
|
|
public class HTTPUtil {
|
|
|
|
|
public static String sendPOSTRequest(String url, String json) {
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
|
|
|
|
// 设置请求头信息
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
|
|
|
|
|
// 构建请求体数据
|
|
|
|
|
|
|
|
|
|
// 创建HttpEntity对象并将请求头和请求体添加进去
|
|
|
|
|
HttpEntity<String> entity = new HttpEntity<>(json, headers);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
|
|
|
|
|
|
|
|
|
|
// 获取返回结果
|
|
|
|
|
String result = response.getBody();
|
|
|
|
|
return result;
|
|
|
|
|
// RestTemplate client = new RestTemplate();
|
|
|
|
|
// HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
// HttpMethod method = HttpMethod.POST;
|
|
|
|
|
// // 以表单的方式提交
|
|
|
|
|
//// headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
|
|
// headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
// // 将请求头部和参数合成一个请求
|
|
|
|
|
// HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
|
|
|
|
|
// // 执行HTTP请求,将返回的结构使用String类格式化
|
|
|
|
|
// ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
|
|
|
|
|
// return response.getBody();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param url 请求地址
|
|
|
|
|
|