0

I'm developing in java an application which verifies if one site is online. I get the http response by:

URL url = new URL("http://www.example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

But when I access the URL it is redirecting and the HTTP response is always 301. I want to get the new URL that the site was redirected. Is it possible?

  • You need to set follow redirect to true and the api will load the redirected page then you can inspect the location header as everyone else suggested. – Minh Kieu Jun 07 '17 at 14:55
  • Possible duplicate of [can't get response header location using Java's URLConnection](https://stackoverflow.com/questions/3786161/cant-get-response-header-location-using-javas-urlconnection) – Daniel Bickler Jun 07 '17 at 14:56
  • 1
    Possible duplicate of [Java - How to find the redirected url of a url?](https://stackoverflow.com/questions/2659000/java-how-to-find-the-redirected-url-of-a-url) – soorapadman Jun 07 '17 at 14:58
  • 1
    HttpURLConnection automatically follows redirects by default. If you get a 301, you must have disabled following redirects explicitly with the `setFollowRedirects` or `setInstanceFollowRedirects` methods, but you didn't mention that in your question. – Erwin Bolwidt Jun 07 '17 at 14:58
  • @ErwinBolwidt the site that I'm trying to get the response give me a 301 and 302 HTTP response. But using the getHeaderField() I only get the old URL – Thales Flores Jun 07 '17 at 15:16
  • @ErwinBolwidt There are some cases where an automatic redirect will not occur, even if HttpURLConnection is configured for it: in particular, a redirect from http to https or from https to http is not considered secure and will not be automatically followed. – VGR Jun 07 '17 at 15:47

2 Answers2

0

The target of the redirection is in the HTTP Header of the response, look for the "Location" header.

Gereon
  • 17,258
  • 4
  • 42
  • 73
  • [getHeaderField](https://docs.oracle.com/javase/8/docs/api/java/net/URLConnection.html#getHeaderField-java.lang.String-) returns the specified header field. – Daniel Bickler Jun 07 '17 at 14:55
0

You can access to the header using this :

String redirectUrl = connection.getHeaderField("Location");
  • I get the URL in the header, but it's the same. I actually get two HTTP response, one is 301 and the other is 302. Is it change the way that I get the header? – Thales Flores Jun 07 '17 at 15:15
  • @ThalesFlores are you sure it's the same URL? Or perhaps it starts with `https` instead of `http`? – RealSkeptic Jun 07 '17 at 15:25
  • The site that I'm accessing is https and when it's redirected going to a http. But when my code runs, I only get the https URL. – Thales Flores Jun 07 '17 at 15:30
  • It's strange, because the method should get the final redirect like for this example : `URL url = new URL("http://www.microsoft.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); String redirectUrl = connection.getHeaderField("Location"); System.out.println(redirectUrl);` – Omar EDDASSER Jun 07 '17 at 15:33
  • @OmarEDDASSER I import the API again, by myself, and now it works. Tks for your attention – Thales Flores Jun 07 '17 at 16:55