HttpURLConnection: Is it necessary to call connect()?

邮差的信 提交于 2019-11-29 18:12:19

问题


Many examples I've seen don't explicitly call connect(). Instead they just use getInputStream() or getResponseCode().

I'm assuming all of these HttpURLConnection methods that require a connection just call connect() themselves?

Are there any cases where connect() must be explicitly called for an HttpURLConnection?


回答1:


No, there are no cases. It's implicitly executed on demand. It's even specified in the documentation. Here's an extract of the URLConnection#connect() javadoc:

Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.




回答2:


connect() is implied and automatically invoked when needed.



来源:https://stackoverflow.com/questions/2792705/httpurlconnection-is-it-necessary-to-call-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!