|
|
|
@ -1,8 +1,25 @@ |
|
|
|
package com.zdxt.enforcementcode.common.util; |
|
|
|
|
|
|
|
import org.apache.http.client.config.RequestConfig; |
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
|
import org.apache.http.client.methods.HttpPost; |
|
|
|
import org.apache.http.config.Registry; |
|
|
|
import org.apache.http.config.RegistryBuilder; |
|
|
|
import org.apache.http.conn.socket.ConnectionSocketFactory; |
|
|
|
import org.apache.http.conn.socket.PlainConnectionSocketFactory; |
|
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier; |
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
|
|
|
import org.apache.http.entity.ContentType; |
|
|
|
import org.apache.http.entity.StringEntity; |
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
import org.apache.http.impl.client.HttpClients; |
|
|
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import javax.net.ssl.SSLContext; |
|
|
|
import javax.net.ssl.TrustManager; |
|
|
|
import javax.net.ssl.X509TrustManager; |
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.DataOutputStream; |
|
|
|
@ -15,8 +32,8 @@ import java.util.Map; |
|
|
|
|
|
|
|
public class HttpUtil { |
|
|
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class); |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送 POST 请求 |
|
|
|
* |
|
|
|
@ -29,44 +46,44 @@ public class HttpUtil { |
|
|
|
log.info("============================== Post请求开始 =============================="); |
|
|
|
log.info("---> url: {}",url); |
|
|
|
try { |
|
|
|
URL uri = new URL(url); |
|
|
|
HttpURLConnection connection = (HttpURLConnection) uri.openConnection(); |
|
|
|
connection.setRequestMethod("POST"); |
|
|
|
connection.setRequestProperty("Content-Type","application/json"); |
|
|
|
connection.setRequestProperty("Charsert","UTF-8"); |
|
|
|
for (Map.Entry<String, Object> entry : heardMap.entrySet()) { |
|
|
|
// 对value进行相应的操作
|
|
|
|
connection.setRequestProperty(entry.getKey(), String.valueOf(entry.getValue())); |
|
|
|
} |
|
|
|
connection.setDoOutput(true); |
|
|
|
DataOutputStream out = new DataOutputStream(connection.getOutputStream()); |
|
|
|
// out.writeBytes(params);
|
|
|
|
out.write(params.getBytes(StandardCharsets.UTF_8)); //解决中文乱码
|
|
|
|
out.flush(); |
|
|
|
out.close(); |
|
|
|
int responseCode = connection.getResponseCode(); |
|
|
|
if(responseCode == HttpURLConnection.HTTP_OK){ |
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); |
|
|
|
String inputLine; |
|
|
|
StringBuffer response = new StringBuffer(); |
|
|
|
|
|
|
|
while ((inputLine = in.readLine()) != null){ |
|
|
|
response.append(inputLine); |
|
|
|
// 内网用 IP 访问时证书是 *.szssfj.cn,与 GET 保持一致:信任所有证书并跳过主机名校验
|
|
|
|
SSLContext sc = SSLContext.getInstance("TLSv1.2"); |
|
|
|
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom()); |
|
|
|
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() |
|
|
|
.register("http", PlainConnectionSocketFactory.INSTANCE) |
|
|
|
.register("https", new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE)) |
|
|
|
.build(); |
|
|
|
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry); |
|
|
|
try (CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(manager).build()) { |
|
|
|
HttpPost httpPost = new HttpPost(url); |
|
|
|
RequestConfig config = RequestConfig.custom() |
|
|
|
.setConnectTimeout(30000) |
|
|
|
.setSocketTimeout(30000) |
|
|
|
.build(); |
|
|
|
httpPost.setConfig(config); |
|
|
|
httpPost.setHeader("Content-Type", "application/json"); |
|
|
|
for (Map.Entry<String, Object> entry : heardMap.entrySet()) { |
|
|
|
httpPost.setHeader(entry.getKey(), String.valueOf(entry.getValue())); |
|
|
|
} |
|
|
|
httpPost.setEntity(new StringEntity(params, ContentType.create("application/json", StandardCharsets.UTF_8))); |
|
|
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { |
|
|
|
int responseCode = response.getStatusLine().getStatusCode(); |
|
|
|
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) { |
|
|
|
log.info("post请求成功: {}", body); |
|
|
|
return body; |
|
|
|
} else { |
|
|
|
log.error("============================== Post请求调用失败 =============================="); |
|
|
|
log.error("---> 失败码:{}", responseCode); |
|
|
|
log.error("---> url: {}", url); |
|
|
|
log.error("---> params: {}", params); |
|
|
|
log.error("---> 响应体:{}", body); |
|
|
|
log.error("============================== =========================== =============================="); |
|
|
|
} |
|
|
|
} |
|
|
|
in.close(); |
|
|
|
log.info("post请求成功"+response.toString()); |
|
|
|
return response.toString(); |
|
|
|
}else{ |
|
|
|
log.error("============================== Post请求调用失败 =============================="); |
|
|
|
log.error("---> 失败码:{}",responseCode); |
|
|
|
log.error("---> url: {}",url); |
|
|
|
log.error("---> params: {}",params); |
|
|
|
log.error("---> heardMap: {}",heardMap); |
|
|
|
log.error("============================== =========================== =============================="); |
|
|
|
// BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("Post请求异常, url: {}", url, e); |
|
|
|
} |
|
|
|
|
|
|
|
return ""; |
|
|
|
|