Reputation: 159
Having a tough time understanding URLConnection and where connect() is possibly being called implicitly.
This code doesn't work:
URL url = new URL(firstUrl);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
conn.getHeaderField("Location"); //returns null
connection.setInstanceFollowRedirects(false);
This code works:
URL url = new URL(firstUrl);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setInstanceFollowRedirects(false);
conn.getHeaderField("Location"); //returns the redirect URL
I'm having a tough time understanding this. Is the "getHeaderField" implicitly calling connect? I don't see that noted in the documentation anywhere.
Upvotes: 2
Views: 58
Reputation: 767
It actually does connect, see http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/sun/net/www/protocol/http/HttpURLConnection.java#HttpURLConnection.getHeaderField%28java.lang.String%29 It should be mentioned in the docs, I agree...
Upvotes: 2