This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes How to detect URL forwarding with java.net.URL class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "How to detect URL forwarding with java.net.URL class" Watch "How to detect URL forwarding with java.net.URL class" New topic
Author

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

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

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

Joined: Sep 28, 2004
Posts: 16815
    
  19

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
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

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

Joined: Sep 28, 2004
Posts: 16815
    
  19

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
Marshal

Joined: Mar 22, 2005
Posts: 35444
    
    9
The header to look for in case of a 30x status is called "Location".


Android appsImageJ pluginsJava web charts
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to detect URL forwarding with java.net.URL class
 
Similar Threads
request origin
Detect loading of ImageIcon from URL?
Java URL Object and client-side translation
How i can check an string to be a correct web site format?
RequestDispatcher not working properly in Websphere