HttpURLConnection:是否有必要调用connect()? [英] HttpURLConnection: Is it necessary to call connect()?
问题描述
我看到的许多示例没有显式调用 connect()
。相反,他们只是使用 getInputStream()
或 getResponseCode()
。
Many examples I've seen don't explicitly call connect()
. Instead they just use getInputStream()
or getResponseCode()
.
我假设所有这些HttpURLConnection方法需要一个连接只需调用 connect()
自己?
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?
推荐答案
不,没有情况。它隐含地按需执行。它甚至在文档中指定。以下是 URLConnection#connect()
javadoc:
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:
连接,像
getContentLength
,如果需要,将隐式执行连接。
Operations that depend on being connected, like
getContentLength
, will implicitly perform the connection, if necessary.
这篇关于HttpURLConnection:是否有必要调用connect()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!