|
@@ -17,6 +17,7 @@ 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.DefaultHttpRequestRetryHandler;
|
|
@@ -31,10 +32,12 @@ import org.slf4j.MDC;
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
import java.net.SocketTimeoutException;
|
|
|
import java.nio.charset.Charset;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.security.KeyManagementException;
|
|
|
import java.security.KeyStoreException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
@@ -94,14 +97,33 @@ public class HttpPoolClient {
|
|
|
|
|
|
|
|
|
public Optional<String> postJson(String url, String json) {
|
|
|
+// HttpPost httpPost = new HttpPost(url);
|
|
|
+// if (StringUtils.isBlank(json)) {
|
|
|
+// return request(httpPost);
|
|
|
+// }
|
|
|
+// StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
|
|
|
+// entity.setContentEncoding("UTF-8");
|
|
|
+// entity.setContentType("application/json");
|
|
|
+// httpPost.setEntity(entity);
|
|
|
+// return request(httpPost);
|
|
|
+ return postJson(url, json, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Optional<String> postJson(String url, String json, Map<String, String> headers) {
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 添加自定义请求头
|
|
|
+ if (headers != null && !headers.isEmpty()) {
|
|
|
+ for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ httpPost.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
if (StringUtils.isBlank(json)) {
|
|
|
return request(httpPost);
|
|
|
}
|
|
|
- StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
|
|
|
- entity.setContentEncoding("UTF-8");
|
|
|
- entity.setContentType("application/json");
|
|
|
+ // 正确设置 Content-Type 和字符集
|
|
|
+ StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8));
|
|
|
httpPost.setEntity(entity);
|
|
|
+
|
|
|
return request(httpPost);
|
|
|
}
|
|
|
|