• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to detect URL forwarding with java.net.URL class  RSS feed

 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the java.net.URL object, for both GET and POST pages. And for the most part, everything works. However, with some pages, the URL will forward to another page, and I wind up reading zero bytes, from the URL object.

Question: How do a detect that the page has been forwarded? And how do I know what URL, has it been forwarded to? (so that I can follow it)

Thanks,
Henry
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cast your URLConnection object to an HttpURLConnection then you can call the setInstanceFollowRedirects(false) method. Not sure what happens when a redirect happens, I haven't used this method myself, but you could experiment with known redirecting URLs.

Edit: but I see you are asking about forwarding, not redirecting. You shouldn't have a problem of not being able to read data if the server forwards your request. If you do, maybe there's something wrong on the server. At any rate there's no way for the client to find out about forwarding.
[ August 18, 2008: Message edited by: Paul Clapham ]
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I think it is a redirect, as the page URL does change. I don't want to turn off the redurect, I just want to know if it happens, and what the new URL is....

Baically, if a URL redirect happens, I have to parse and deal with the URL differently. But is not currently able to detect it, hence, dealing with the URL incorrectly.

Thanks,
Henry
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the only way for you to know it happens is to be given the 302 (or similar) code as an exception, and to get the new URL from one of the returned headers. Then you'd have to send the new request yourself.

By the way POST requests can't be redirected, because a redirect always requires the client to send a GET request as a response to the 302 code.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, I have a GET page, that periodically does a redirect to a POST page, in order to do authentication... so... in pseudo code, I guess that I need to do this....



I am pretty sure that I am missing something, and will chime in again with a followup question.

Thanks,
Henry
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The header to look for in case of a 30x status is called "Location".
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. It pretty much worked exactly as prescribed in the pseudo code. The only hiccup was... the POST page was defined in the CSS file, which I had to hunt down.

Thanks,
Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!