HttpURLConnection implementation

前端 未结 6 1119
自闭症患者
自闭症患者 2020-12-09 10:22

I have read that HttpURLConnection supports persistent connections, so that a connection can be reused for multiple requests. I tried it and the only way to send a second PO

6条回答
  •  青春惊慌失措
    2020-12-09 10:26

    Should input/output stream be closed followed by a url.openConnection(); each time to send the new request (avoiding disconnect())?

    Yes.

    If yes, I can not see how the connection is being reused when I call url.openConnection() for the second time, since the connection has been removed from the cache for the first request and can not find how it is returned back.

    You are confusing the HttpURLConnection with the underlying Socket and its underlying TCP connection. They aren't the same. The HttpURLConnection instances are GC'd, the underlying Socket is pooled, unless you call disconnect().

提交回复
热议问题