cjcamisa
cjcamisa

Reputation: 159

URLConnection opening weirdness

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

Answers (1)

Related Questions