URLConnection开放怪异

时间:2016-01-07 17:34:16

标签: java http url httpurlconnection

很难理解URLConnection以及可能隐式调用connect()的地方。

此代码无效:

URL url = new URL(firstUrl);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
conn.getHeaderField("Location"); //returns null
connection.setInstanceFollowRedirects(false);

此代码有效:

URL url = new URL(firstUrl);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setInstanceFollowRedirects(false);
conn.getHeaderField("Location"); //returns the redirect URL

我很难理解这一点。是" getHeaderField"隐式调用连接?我在任何地方都没有看到文档中提到的内容。