I want to get the URL that the I'm feeling lucky button redirects to (aka the first result). I pass in the url:
http://www.google.com/search?&btnI=745&pws=0&q=hello
which by now, goes to http://www.hellomagazine.com/
The problem is that the server responds with code 200 (OK) instead of 302 (redirection), so I don't know how the redirection is being performed, nor how to get the final URL.
This is the code I've been trying:
HttpURLConnection connection = (HttpURLConnection)wikiURL.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/4.76");
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
connection.setInstanceFollowRedirects(false);
connection.connect();
System.out.println(connection.getResponseCode());
Syst开发者_如何学Cem.out.println(connection.getHeaderField("Location"));
This is the output:
200
null
Edit: The problem seems to be the url itself. It works with the one I posted here, but not with this one, for instance:
"http://www.google.com/search?&btnI=745&pws=0&q=%2Bfutebolista+%2Bwikipedia+Marcio+Gabriel,+Atlético-GO"
If I run your code, I get this output:
302
http://www.hellomagazine.com/
If I set
connection.setInstanceFollowRedirects(true);
then I can read the full Hello website from the
connection.getInputStream()
If I leave it as false, i.e. not follow redirects, then the output is:
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.hellomagazine.com/">here</A>.
</BODY></HTML>
Which is a little wierd, coz the header you are reading isn't actually set! You could parse that body though and look for the HREF when you get a 302, if it still isn't working for you.
If you are still getting a 200 response, then try this and let us know what the output is please:
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
System.out.println(sb.toString());
I also setup Eclipse's TCP/IP Monitor so that I could see exactly what was being sent over the wire.
connection.setInstanceFollowRedirects(true);
is your problem. You want this to be false because you don't want to auto-follow redirects; you want to retrieve the notification of the re-direct.
Problem was encoding, had to force UTF-8. I was showing me 200 code because there were no results found when searching for things with latin characters that weren't properly encoded
精彩评论