Different behavior between my java app and my navigator when querying an url
I would like to query a portlet from a meteo service. But as my navigator giv开发者_StackOverflow社区es me the correct page, my java application does not.
Here is my code without try/catch:
public static void main(String[] args) {
// The portlet URL
String cookieUrl = "http://france.meteofrance.com/france/meteo?PREVISIONS_PORTLET.path=previsionspluie/290190";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
// Some proxy configuration
// Basic properties set to perform my task, but they might be useless as it does not work as I would
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12");
System.out.println(handler.getHtml(reader));
}
private String getHtml(BufferedReader reader)
{
String html = "";
for (String line; (line = reader.readLine()) != null;)
html += line + "\n";
return html;
}
You can test it with the portle url given as example. In the navigator the response contains the correct "prevision de pluie pour Brest". In the java app the response contains this page : http://france.meteofrance.com/NoCookie.htm
It seems to be a cookie matter. But how could I handle it as my first tries to get cookie and send them back were unsuccessful.
Any help please?
It looks like the site you are trying to access relies on Cookies which are not supported by HttpURLConnection. A way around this issue is to use a library like HtmlUnit which simulates a browser (supports cookies, javascript, etc..).
精彩评论