0

问题在哪里?当对 HTTPClient 3.1 使用相同的请求和设置时,会保持持久连接,但不会在HTTPClient 4.x [DefaultClient].

javax.net.ssl.SSLContext ctx = javax.net.ssl.SSLContext.getInstance("TLS");
ctx.init(keyfactory.getKeyManagers(), tmfactory.getTrustManagers(), new    SecureRandom());
..
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", 443, new MySSLFactory(ctx))); 
PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
cm.setMaxTotal(20);
cm.setDefaultMaxPerRoute(5);
cm.setMaxPerRoute(new HttpRoute(localhost), 5);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000); // Connection timeout
HttpConnectionParams.setSoTimeout(httpParameters, 0);
HttpClient client = new DefaultHttpClient(cm,httpParameters);           
while (i<200) {
    HttpPost post = new HttpPost(send.getUrl().toString());
    Header[] heads =  post.getAllHeaders();
    System.out.println("Request Headers size:"+heads.length);
    for (int ii=0; ii<heads.length; ii++) {
        Header head = heads[ii];
        System.out.println("Name:"+head.getName()+",  value:"+head.getValue()); 
    }       
    ContentType contentType =     ContentType.create("text/xml","UTF-8");       
    HttpEntity entity = new StringEntity(data,contentType );
    post.setEntity(entity);
    HttpResponse resp = client.execute(post);
...
}
client.getConnectionManager().shutdown();

这是日志输出:

2013/04/05 10:52:12:337 CEST [DEBUG] PoolingClientConnectionManager - Connection request: [route:  {s}->https://smsconnector.cz][total kept alive: 0; route allocated: 0 of 5; total allocated: 0 of 5] 
2013/04/05 10:52:12:400 CEST [DEBUG] PoolingClientConnectionManager - Connection leased: [id: 0][route:  {s}->https://smsconnector.cz][total kept alive: 0; route allocated: 1 of 5; total allocated: 1 of 5] 
2013/04/05 10:52:13:294 CEST [DEBUG] DefaultClientConnectionOperator - Connecting to smsconnector.cz:443 
2013/04/05 10:52:13:382 CEST [DEBUG] RequestAddCookies - CookieSpec selected: best-match 
2013/04/05 10:52:13:404 CEST [DEBUG] RequestAuthCache - Auth cache not set in the context 
2013/04/05 10:52:13:405 CEST [DEBUG] RequestTargetAuthentication - Target auth state: UNCHALLENGED 
2013/04/05 10:52:13:405 CEST [DEBUG] RequestProxyAuthentication - Proxy auth state: UNCHALLENGED 
2013/04/05 10:52:13:405 CEST [DEBUG] DefaultHttpClient - Attempt 1 to execute request 
2013/04/05 10:52:13:405 CEST [DEBUG] DefaultClientConnection - Sending request: POST  /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1 
2013/04/05 10:52:13:406 CEST [DEBUG] wire - >> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1[\r][\n]" 
2013/04/05 10:52:13:407 CEST [DEBUG] wire - >> "Connection: Keep-Alive[\r][\n]" 
2013/04/05 10:52:13:407 CEST [DEBUG] wire - >> "Keep-Alive: timeout=5, max=5[\r][\n]" 
2013/04/05 10:52:13:407 CEST [DEBUG] wire - >> "Content-Length: 222[\r][\n]" 
2013/04/05 10:52:13:407 CEST [DEBUG] wire - >> "Content-Type: text/xml; charset=UTF-8[\r][\n]" 
2013/04/05 10:52:13:408 CEST [DEBUG] wire - >> "Host: smsconnector.cz[\r][\n]" 
2013/04/05 10:52:13:408 CEST [DEBUG] wire - >> "[\r][\n]" 
2013/04/05 10:52:13:408 CEST [DEBUG] headers - >> POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1 
2013/04/05 10:52:13:408 CEST [DEBUG] headers - >> Connection: Keep-Alive 
2013/04/05 10:52:13:408 CEST [DEBUG] headers - >> Keep-Alive: timeout=5, max=5 
2013/04/05 10:52:13:408 CEST [DEBUG] headers - >> Content-Length: 222 
2013/04/05 10:52:13:408 CEST [DEBUG] headers - >> Content-Type: text/xml; charset=UTF-8 
2013/04/05 10:52:13:408 CEST [DEBUG] headers - >> Host: smsconnector.cz 
2013/04/05 10:52:13:408 CEST [DEBUG] wire - >> "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:b2b="http://b2b.et.cz"><soapenv:Header/><soapenv:Body><b2b:receive><baID>123456</baID></b2b:receive></soapenv:Bod y></soapenv:Envelope>" 
2013/04/05 10:52:43:812 CEST [DEBUG] wire - << "HTTP/1.1 200 OK[\r][\n]" 
2013/04/05 10:52:43:815 CEST [DEBUG] wire - << "Date: Fri, 05 Apr 2013 08:52:13 GMT[\r][\n]" 
2013/04/05 10:52:43:815 CEST [DEBUG] wire - << "Server: Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8h Resin/3.1.9[\r][\n]" 
2013/04/05 10:52:43:815 CEST [DEBUG] wire - << "Content-Type: text/xml; charset=UTF-8[\r][\n]" 
2013/04/05 10:52:43:815 CEST [DEBUG] wire - << "Vary: Accept-Encoding[\r][\n]" 
2013/04/05 10:52:43:815 CEST [DEBUG] wire - << "Keep-Alive: timeout=60, max=2000[\r][\n]" 
2013/04/05 10:52:43:816 CEST [DEBUG] wire - << "Connection: Keep-Alive[\r][\n]" 
2013/04/05 10:52:43:816 CEST [DEBUG] wire - << "Transfer-Encoding: chunked[\r][\n]" 
2013/04/05 10:52:43:816 CEST [DEBUG] wire - << "[\r][\n]" 
2013/04/05 10:52:43:817 CEST [DEBUG] DefaultClientConnection - Receiving response: HTTP/1.1 200 OK 
2013/04/05 10:52:43:817 CEST [DEBUG] headers - << HTTP/1.1 200 OK 
2013/04/05 10:52:43:817 CEST [DEBUG] headers - << Date: Fri, 05 Apr 2013 08:52:13 GMT  
2013/04/05 10:52:43:818 CEST [DEBUG] headers - << Server: Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8h Resin/3.1.9 
2013/04/05 10:52:43:818 CEST [DEBUG] headers - << Content-Type: text/xml; charset=UTF-8  
2013/04/05 10:52:43:818 CEST [DEBUG] headers - << Vary: Accept-Encoding  
2013/04/05 10:52:43:818 CEST [DEBUG] headers - << Keep-Alive: timeout=60, max=2000  
2013/04/05 10:52:43:818 CEST [DEBUG] headers - << Connection: Keep-Alive  
2013/04/05 10:52:43:818 CEST [DEBUG] headers - << Transfer-Encoding: chunked  
2013/04/05 10:52:43:822 CEST [DEBUG] DefaultHttpClient - Connection can be kept alive for 60000 MILLISECONDS  
2013/04/05 10:52:43:825 CEST [DEBUG] wire - << "108[\r][\n]"  
2013/04/05 10:52:43:825 CEST [DEBUG] wire - << "<soap:Envelope  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:receiveResponse xmlns:ns2="http://b2b.et.cz"><result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:nil="true"/></ns2:receiveResponse></soap:Body></soap:Envelope>" 
2013/04/05 10:52:43:825 CEST [DEBUG] wire - << "[\r][\n]"  
2013/04/05 10:52:43:825 CEST [DEBUG] wire - << "0[\r][\n]"  
2013/04/05 10:52:43:825 CEST [DEBUG] wire - << "[\r][\n]"  
2013/04/05 10:52:43:826 CEST [DEBUG] PoolingClientConnectionManager - Connection [id: 0][route:  {s}->https://smsconnector.cz][state: EMAILADDRESS=lab@draft.cz, CN=EndToEnd Test, OU=VAS,  O="EndToEnd Test, ICO: 00000000", L="Jecna 1, Praha 2, 123", ST=., C=CZ] can be kept alive for 60000 MILLISECONDS  
2013/04/05 10:52:43:826 CEST [DEBUG] PoolingClientConnectionManager - Connection released: [id: 0][route:  {s}->https://smsconnector.cz][state: EMAILADDRESS=lab@draft.cz, CN=EndToEnd Test, OU=VAS,  O="EndToEnd Test, ICO: 00000000", L="Jecna 1, Praha 2, 123", ST=., C=CZ][total kept alive: 1; route allocated: 1 of 5;  total allocated: 1 of 5] 
2013/04/05 10:52:43:826 CEST [DEBUG] PoolingClientConnectionManager - Connection request: [route:  {s}->https://smsconnector.cz][total kept alive: 1; route allocated: 1 of 5; total allocated: 1 of 5]  
2013/04/05 10:52:43:826 CEST [DEBUG] PoolingClientConnectionManager - Connection leased: [id: 1][route:  {s}->https://smsconnector.cz][total kept alive: 1; route allocated: 2 of 5; total allocated: 2 of 5] 

不同的会话 ID

默认客户端

>  Operation/CN=EndToEnd Test/emailAddress=lab@draft.cz
> C2730AFB1EE8A769F642861F3E167FC961A115E540BF06D2CDB21FC75921758E
> SUCCESS [05/Apr/2013:11:12:29 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> C5520D9FCA38E1A6BEB5994A4EBA7F9EA28165AE51C8A0976F6B137C93A0128D
> SUCCESS [05/Apr/2013:11:12:59 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> 431FD618F6093BD94B0DF845765DC96F45A8B7F499CEC0EA1BD66CE6FE9F12E4
> SUCCESS [05/Apr/2013:11:13:30 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> F6EA63B12ADB023FCD13E88B44CAE86D1A5AD4B28019783B331EDBBF6F8106F8
> SUCCESS [05/Apr/2013:11:14:00 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> CAF1B53A029112EDC0CF7124ACB0D8EE662312A9CB672C96190EBF60F7ADD6FF
> SUCCESS [05/Apr/2013:11:14:30 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> 3E212288D658D95C69EA7001588D0A9DD9113F8CD0CED406BCE5658076628B2F
> SUCCESS

HTTP客户端 3.1

Session-id 相同 - 持久连接

> [05/Apr/2013:11:15:18 +0200] 160.218.43.253 TLSv1 RC4-MD5 "POST
> /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> F46C2DE76C881FD21B7235F4E3BCC778358BAF73687A433A7E4E46A3A9DCF539
> SUCCESS [05/Apr/2013:11:15:49 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> F46C2DE76C881FD21B7235F4E3BCC778358BAF73687A433A7E4E46A3A9DCF539
> SUCCESS [05/Apr/2013:11:16:22 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> F46C2DE76C881FD21B7235F4E3BCC778358BAF73687A433A7E4E46A3A9DCF539
> SUCCESS [05/Apr/2013:11:16:52 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> F46C2DE76C881FD21B7235F4E3BCC778358BAF73687A433A7E4E46A3A9DCF539
> SUCCESS [05/Apr/2013:11:17:41 +0200] 160.218.43.253 TLSv1 RC4-MD5
> "POST /smsconnector/services/PPGwPort?METHOD=POST HTTP/1.1" 264
> /C=CZ/ST=./L=Jecna 1, Praha 2, 123/O=EndToEnd Test, ICO:
> 00000000/OU=VAS/CN=EndToEnd Test/emailAddress=lab@draft.cz
> F46C2DE76C881FD21B7235F4E3BCC778358BAF73687A433A7E4E46A3A9DCF539
> SUCCESS
4

1 回答 1

0

我不是 100% 确定,但你能尝试保持本地环境吗?

HttpClient client = new DefaultHttpClient(cm,httpParameters);
// Create local context
HttpContext context= new BasicHttpContext();
while (i<200) {
    HttpPost post = new HttpPost(send.getUrl().toString());
    Header[] heads =  post.getAllHeaders();
    System.out.println("Request Headers size:"+heads.length);
    for (int ii=0; ii<heads.length; ii++) {
        Header head = heads[ii];
        System.out.println("Name:"+head.getName()+",  value:"+head.getValue()); 
    }       
    ContentType contentType =     ContentType.create("text/xml","UTF-8");       
    HttpEntity entity = new StringEntity(data,contentType );
    post.setEntity(entity);
    // Pass context for execution
    HttpResponse resp = client.execute(post, context);
...
}
于 2013-04-05T11:42:51.650 回答