• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

Redirecting to new page after loging using HttpsURLConnection

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
 
0
lez60
Asked:
lez60
  • 4
1 Solution
 
hoomanvCommented:
not sure but use
window.location="http://finance.yahoo.com";
instead
0
 
hoomanvCommented:
setFollowRedirects() should be invoked before url.openConnection() otherwise it wont work then you have to call setInstanceFollowRedirects()
0
 
hoomanvCommented:
man, dont you think that HttpUrlConnection doesn't parse the html content and it cant understand javascript ? you need to do it manualy
0
 
hoomanvCommented:
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
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now