Tuesday 10 September 2013

How to Ensure Input from URL isn't from a Redirected Page

How to Ensure Input from URL isn't from a Redirected Page

I have the following lines of code that gathers the source code from a
given URL:
URL url = new URL(websiteAddress);
URLConnection connection = url.openConnection(); // throws an IOException
connection.setConnectTimeout(timeoutInMilliseconds);
bufferedReader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
outputString += line;
}
However, the problem that I'm having is that wi-fi hotspots often redirect
you to a page where you have to click "I Agree." If you run this code
before you have clicked that checkbox, then it gathers the source code
from the hotspot login page, rather than the intended page.
What I want to do is have some way of checking whether or not the intended
page was reached. I was hoping that calling connection.getURL() after
creating the InputStreamReader would show me the actual web page that was
arrived, but no such luck. How can I determine whether or not the intended
URL has been redirected?

No comments:

Post a Comment