We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Redirecting to new page after loging using HttpsURLConnection

lez60 asked
Medium Priority
Last Modified: 2008-01-09
I have following task:
1. login to yahoo
2. after successful login go to finance.yahoo.com
3. parse some information e.g. stock quote
4. I want to repeat operation each 5 minutes to get the same , refreshed page, so do I have to re-establish connection or reuse existing one with refresh mechanizm ?

I was able to finish 1. connect but result looks like this:
<script language="JavaScript">
// -->
<meta http-equiv="Refresh" content="0; url=http://finance.yahoo.com">
If you are seeing this page, your browser settings prevent you
from automatically redirecting to a new URL.
Please <a href="http://finance.yahoo.com">click here</a> to continue.

so it looks, after login i am not redirecting but in my code
I am using HttpsURLConnection.setFollowRedirects(true);
so It shoud work. My guess is , that javscript is not executed.

thanks in advance for any help

Watch Question

Top Expert 2006

not sure but use
Top Expert 2006

setFollowRedirects() should be invoked before url.openConnection() otherwise it wont work then you have to call setInstanceFollowRedirects()
Top Expert 2006

man, dont you think that HttpUrlConnection doesn't parse the html content and it cant understand javascript ? you need to do it manualy
Top Expert 2006
as a last comment
I found that setFolowRedirects just follows the http redirect instructions
i.e in php you use header('Location: '. $to);  to send a raw http header
setFolowRedirects is by default true
I have tested it on my local apache server and it worked fine (no need to serFolowRedirects(true))
the page you are viewing is attempting to redirect with two different mechanisms a javascript and an html (<meta http-equiv="Refresh") and non of them is understandable by HttpUrlConnection alone,
and the only way is to parse the html result and find window.location is assigned to what and the go there manualy

assume this code
HttpURLConnection con = (HttpURLConnection) HOST.openConnection();
con.setInstanceFollowRedirects(false); // as i told dont use static method setFollowRedirects() after openconnection

then if you look through header fileds --> con.getHeaderFieldKey(index) and con.getHeaderField(index);
you can see a filed with name:location and value:redirect location
it is through this header that HttpURLConnection realizes redirection and no other way

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.