Martin08
Martin08

Reputation: 21460

how to get new URL in a http 302 redirection in Java?

I came across a HTTP 302 page that say Location: //-now-playing.html (which looks strange because usually it is the full url, i.e. Location: http://www.somewhere.else) and that causes my HttpURLConnection con with con.setFollowRedirects(true) to fail. Yet, my browser does not fail with this instruction. How can I get the true URL in this case?

You can see the full request and response in this case by trying this url http://cinemaclock.com/showtimes/ont/Toronto/43844/The_Trip in www.web-sniffer.net . Thank you.

EDIT: Thank you all. I found out this is a case of bad URL. False alarm, but thank you all.

Upvotes: 1

Views: 4532

Answers (4)

user756212
user756212

Reputation: 522

You may have just a bad return, or you have a an absolte location redirect, without its hostname, going on here.

While the standard says that a location response should have a full absoulteURI, most browsers and software will handle without a problem the relative location response.

This one has two forward slashes in front of it, which is certainly bad form, but most servers will automatically assume that two // together in a URL really mean just one, so the server will generally serve the same page if you were to say ask for http://www.xyz.com/index.html and ask for http://www.xyz.com//index.html.

Now, to fix your problem, if this is indeed meant as a redirect with a relative URL, you will need to build the absolute URL from the header information. First, since its relative location redirect its safe to say the Protocol is the same one you were using HTTP, so that's the first part of the URL you should try:

http://

The next part you need is the HOSTNAME, this should be in the HOST variable of the HTTP Header response.. (www.xyz.com)

http://www.xyz.com

and finally path, which is simply what the location of the header responded with appended to the above

http://www.xyz.com/-now-playing.html

Easiest way to test is to simply launch a browser and try to see if that request gets you anywhere. To me that looks like its a bad URL because the fact the html file name starts with a hyphen, which is unusual form, but I would at least TRY to see if building the URL out gets you something.

Good luck.

Upvotes: 0

Eric Suh
Eric Suh

Reputation: 138

First, at least on my computer, Safari and Chrome both fail on this redirect, so it seems the most common behavior is just to return an error and fail, which may be the behavior you might actually want to return to the user.

If you still want to interpolate the proper URL instead of failing, you'd probably have to do a special case parsing of the Location: redirect field to account for the broken URL.

Upvotes: 0

André Laszlo
André Laszlo

Reputation: 15537

That's an error and you need to handle it yourself. You can get the location by:

con.getHeaderField("Location");

Upvotes: 1

Platinum Azure
Platinum Azure

Reputation: 46183

You're doing the right thing by looking at the value of the Location: header.

Unfortunately, you're just getting a bad URL here! :-(

Upvotes: 0

Related Questions