|
|
@@ -14,6 +14,7 @@ import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.security.SecureRandom;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Map;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
@@ -22,10 +23,26 @@ import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
import org.apache.http.entity.mime.content.FileBody;
|
|
|
import org.apache.http.entity.mime.content.StringBody;
|
|
|
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
-
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+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.entity.ContentType;
|
|
|
+import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
+import org.apache.http.entity.mime.content.FileBody;
|
|
|
+import org.apache.http.entity.mime.content.StringBody;
|
|
|
+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 java.io.File;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Map;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.util.regex.Pattern;
|
|
|
@@ -63,8 +80,34 @@ public class HttpUtils {
|
|
|
public static Map<String,Object> uploadFile(String url, File zipFile, String resCode,
|
|
|
String username, String password,String ver,
|
|
|
String filename) {
|
|
|
-
|
|
|
- try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
+ // 创建自定义配置的 HttpClient
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom()
|
|
|
+ .setConnectTimeout(30000) // 连接超时 30秒
|
|
|
+ .setSocketTimeout(120000) // 数据上传需要更长时间
|
|
|
+ .setConnectionRequestTimeout(10000)
|
|
|
+ // 重要:禁用代理检测
|
|
|
+ .setProxyPreferredAuthSchemes(Collections.emptyList())
|
|
|
+ .setTargetPreferredAuthSchemes(Collections.emptyList())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 创建连接管理器
|
|
|
+ PoolingHttpClientConnectionManager connManager =
|
|
|
+ new PoolingHttpClientConnectionManager();
|
|
|
+ connManager.setMaxTotal(100);
|
|
|
+ connManager.setDefaultMaxPerRoute(20);
|
|
|
+
|
|
|
+ try (CloseableHttpClient httpClient = HttpClients.custom()
|
|
|
+ .setConnectionManager(connManager)
|
|
|
+ .setDefaultRequestConfig(requestConfig)
|
|
|
+ // 禁用重试,避免超时后重试
|
|
|
+ .disableAutomaticRetries()
|
|
|
+ // 明确设置不自动重定向
|
|
|
+ .disableRedirectHandling()
|
|
|
+ // 重要:禁用Cookie管理
|
|
|
+ .disableCookieManagement()
|
|
|
+ // 重要:设置连接重用策略
|
|
|
+ .setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE)
|
|
|
+ .build()) {
|
|
|
|
|
|
// 构建基础URL(包含RESCODE, Ver, USER)
|
|
|
String baseUrl = String.format("%s?RESCODE=%s&Ver=%s&USER=%s",
|
|
|
@@ -84,8 +127,15 @@ public class HttpUtils {
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(finalUrl);
|
|
|
|
|
|
+ // 重要:添加 User-Agent
|
|
|
+ httpPost.setHeader("User-Agent", "Mozilla/5.0 HttpClient");
|
|
|
+
|
|
|
+ // 重要:明确设置 Connection 头
|
|
|
+ httpPost.setHeader("Connection", "close");
|
|
|
+
|
|
|
// 构建 multipart/form-data 请求体
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
+ builder.setCharset(StandardCharsets.UTF_8);
|
|
|
|
|
|
// 1. file参数 (Body, binary)
|
|
|
if (zipFile != null && zipFile.exists()) {
|