Per decisione degli sviluppatori Java (motivi di sicurezza), l'oggetto
HttpURLConnection
non segue i redirect HTTP. E' compito di scrive il codice assicurarsi di leggere
dall'header HTTP eventuali redirect e seguirli. Il codice seguente mostra come
seguire redirect 3xx.
/**
* Extract the content from an HTTP URL with an HTTP redirect (301, 302, 303)
* @param url the URL to read
* @return the content from an HTTP URL with an HTTP redirect (301, 302, 303)
*/
public String getUrlContentWithRedirect(String url){
try {
HttpURLConnection conn = this.openConnection(new URL(url));
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = this.openConnection(new URL(newUrl));
conn.setRequestProperty("Cookie", cookies);
}
//read the connection
return this.getContentFromConnection(conn);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*
* Open a HttpURLConnection from a URL object
*/
private HttpURLConnection openConnection(URL obj) throws IOException{
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
return conn;
}