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

x

Redirecting to new page after loging using HttpsURLConnection

lez60
lez60 asked
on
Medium Priority
393 Views
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:
<html>
<head>
<script language="JavaScript">
<!--
window.location.replace("http://finance.yahoo.com");
// -->
</script>
<meta http-equiv="Refresh" content="0; url=http://finance.yahoo.com">
</head>
<body>
If you are seeing this page, your browser settings prevent you
from automatically redirecting to a new URL.
<p>
Please <a href="http://finance.yahoo.com">click here</a> to continue.
</body>
</html>



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


Lez
 
Comment
Watch Question

Top Expert 2006

Commented:
not sure but use
window.location="http://finance.yahoo.com";
instead
Top Expert 2006

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

Commented:
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
Commented:
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))
so
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.

OR

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.