I' trying to implement a Java program that downloads for a given image-URL the HTML source code of Google's similar image search. So if you search for similar android images, you have to open http://ift.tt/19KDKUbhttp://ift.tt/1iXnRIR . Then you will be redirected to the result HTML page.
This is my Java code:
URL url = new URL("http://ift.tt/19KDLap");
// open first connection
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0");
c.connect();
System.out.println("Response Code: " + c.getResponseCode());
// save new Redirect-URL and cookies
String newURL = c.getHeaderField("Location");
String cookies = c.getHeaderField("Set-Cookie");
System.out.println("New URL: " + newURL);
// open the new connnection
url = new URL(newURL);
c = (HttpURLConnection) url.openConnection();
c.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0");
c.setRequestProperty("Cookie", cookies);
System.out.println("Response Code: " + c.getResponseCode());
// get HTML
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
String html = "";
while ((inputLine = bufferedReader.readLine()) != null) {
html += inputLine;
}
bufferedReader.close();
System.out.println(html);
I get the right redirect-URL, but I don't get the right HTML. I get only the html of the start page of Google. Other similar questions at this topic like this doesn't mention this problem.
Aucun commentaire:
Enregistrer un commentaire