HttpsUrlConnection和keep

HttpsUrlConnection和keep

本文介绍了HttpsUrlConnection和keep-alive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我当前的项目中使用 com.sun.net.httpserver.HttpsServer 处理客户端身份验证等。目前它只打印出客户端地址/端口,以便我可以检查是否有一个TCP连接用于多个请求( keep-alive )或者是否为每个请求建立了新连接(因此是新的每次都进行SSL握手)。当我使用FireFox对服务器发出多个请求时,我可以看到keep-alive正在运行。所以服务器部分可以正常使用GET和POST请求。

I am using com.sun.net.httpserver.HttpsServer in my current project which deals with client-authentification etc.. Currently it only prints out the clients address/port, so that I can check if one TCP-connection is used for multiple requests (keep-alive) or if a new connection is established for every request (and thus a new SSL-handshake is made every time). When I use FireFox to make multiple request against the server I can see that keep-alive is working. So the server part works fine with GET and POST-requests.

如果我使用 HttpURLConnection 来提出请求服务器(在这种情况下使用 SSL) keep-alive 也可以工作:只为多个顺序启动的请求建立了一个连接。

If I use HttpURLConnection to make a request against the Server (in this case using no SSL) keep-alive works, too: Only one connection is established for multiple sequentially started requests.

但如果我使用 HttpsURLConnection (使用完全相同的代码,但使用 SSL)则 keep-alive 不再有效了。因此,对于每个请求,建立一个新连接,虽然我使用相同的 SSLContext (以及 SSLSocketFactory ):

But if I use HttpsURLConnection (using exactly the same code, but using SSL) then keep-alive is not working anymore. So for each request a new connection is established, although I am using the same SSLContext (and SSLSocketFactory):

// URL myUrl = ...
// SSLContext mySsl = ...
HttpsURLConnection conn = (HttpsURLConnection) myUrl.openConnection();
conn.setUseCaches(false);
conn.setSSLSocketFactory(mySsl.getSocketFactory());

conn.setRequestMethod("POST");
// send Data
// receive Data

如何强制 HttpsURLConnection 使用 keep-alive 因为许多请求会导致许多SSL握手,这是一个真正的性能问题?

How do I force HttpsURLConnection to use keep-alive because many requests will lead to many SSL-handshakes which is a real performance issue?

更新(2012-04-02):
而不是每次调用 mySsl.getSocketFactory()我试图缓存 SSLSocketFactory 。但没有改变。问题仍然存在。

Update (2012-04-02):Instead of calling mySsl.getSocketFactory() each time, I tried to cache the SSLSocketFactory. But nothing changed. The problem still exists.

推荐答案

我无法使用 HttpsUrlConnection 。但Apache的HTTP客户端可以很好地处理SSL连接的保持活动状态。

I couldn't get it working with HttpsUrlConnection. But Apache's HTTP client handles keep-alive with SSL connections very well.

这篇关于HttpsUrlConnection和keep-alive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:56