HttpsUrlConnection and keep-alive

后端 未结 8 1588
太阳男子
太阳男子 2020-12-08 02:27

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 addr

8条回答
  •  感动是毒
    2020-12-08 03:22

    I ran into this exact same problem and finally have a solution after some in-depth debugging.

    Http(s)UrlConnection does handle Keep-Alive by default but sockets must be in a very specific condition in order to be reused.

    These are:

    • Input streams must be fully consumed. You must call read on the input stream until it returns -1 and also close it.
    • Settings on the underlying socket must use the exact same objects.
    • You should call disconnect (yes this is counter-intuitive) on the Http(s)URLConnection when done with it.

    In the above code, the problem is:

    conn.setSSLSocketFactory(mySsl.getSocketFactory());
    

    Saving the result of getSocketFactory() to a static variable during initialization and then passing that in to conn.setSSLSocketFactory should allow the socket to be reused.

提交回复
热议问题