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.
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)
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 ]
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.
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.
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. 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