@@ -37,23 +37,34 @@ private ApiClient(String url, String ak, String sk, String region, long timeout)
3737 .build ();
3838 }
3939
40- private ApiClient (String url , String ak , String sk , String region , long timeout , String proxy ) throws MalformedURLException {
41- URL purl = new URL (proxy );
42- String p_protocol = purl .getProtocol (); // 协议(http/https 等)
43- String p_host = purl .getHost (); // 主机名(域名或 IP)
44- int p_port = purl .getPort (); // 显式指定的端口号
45- if (p_port < 0 ) {
46- p_port = purl .getDefaultPort ();// 协议默认端口
47- }
48- HttpHost httpsProxy = new HttpHost (p_host , p_port , p_protocol );
40+ private ApiClient (String url , String ak , String sk , String region , long timeout , String proxy , int connMax ) throws MalformedURLException {
4941 this .url = url ;
5042 this .ak = ak ;
5143 this .sk = sk ;
5244 this .region = region ;
53- this .httpClient = HttpClientBuilder .create ()
54- .setConnectionTimeToLive (timeout , TimeUnit .MILLISECONDS )
55- .setProxy (httpsProxy )
56- .build ();
45+
46+ HttpClientBuilder builder = HttpClientBuilder .create ().setConnectionTimeToLive (timeout , TimeUnit .MILLISECONDS );
47+ if (proxy != null && !proxy .isEmpty ()) {
48+ try {
49+ URL purl = new URL (proxy );
50+ String p_protocol = purl .getProtocol (); // 协议(http/https 等)
51+ String p_host = purl .getHost (); // 主机名(域名或 IP)
52+ int p_port = purl .getPort (); // 显式指定的端口号
53+ if (p_port < 0 ) {
54+ p_port = purl .getDefaultPort ();// 协议默认端口
55+ }
56+ HttpHost httpsProxy = new HttpHost (p_host , p_port , p_protocol );
57+ builder .setProxy (httpsProxy );
58+ } catch (MalformedURLException e ) {
59+ throw new IllegalArgumentException ("Invalid Proxy Info:" + proxy , e );
60+ }
61+ }
62+
63+ if (connMax > 0 ) {
64+ builder .setMaxConnTotal (connMax ).setMaxConnPerRoute (connMax );
65+ }
66+
67+ this .httpClient = builder .build ();
5768 }
5869
5970 /**
@@ -81,8 +92,8 @@ public static ApiClient New(String url, String ak, String sk, String region, lon
8192 * @param timeout 连接超时时间(毫秒)
8293 * @return 客户端实例
8394 */
84- public static ApiClient New (String url , String ak , String sk , String region , long timeout , String proxy ) throws MalformedURLException {
85- return new ApiClient (url , ak , sk , region , timeout , proxy );
95+ public static ApiClient New (String url , String ak , String sk , String region , long timeout , String proxy , int connMax ) throws MalformedURLException {
96+ return new ApiClient (url , ak , sk , region , timeout , proxy , connMax );
8697 }
8798
8899 /**
0 commit comments