2010-12-14 33 views
0

我想從一個meteo服務中查詢一個portlet。但是當我的導航器給我正確的頁面時,我的java應用程序沒有。當我查詢一個url時,我的java應用程序和我的導航器之間的不同行爲

這裏是我的代碼,而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; 
} 

您可以作爲例子給出的portle URL測試。 在導航器中,響應包含正確的「prevision de pluie pour Brest」。 在Java應用程序的響應包含此頁面:http://france.meteofrance.com/NoCookie.htm

這似乎是一個cookie問題。但我怎麼能處理它,因爲我第一次嘗試獲取cookie並將它們發回給他們是不成功的。

請幫忙嗎?

回答

1

它看起來像你試圖訪問的網站依賴於Cookies這是不支持HttpURLConnection。解決這個問題的一個方法是使用模擬瀏覽器的類似HtmlUnit的庫(支持cookie,javascript等)。

+0

謝謝你的答案,我會嘗試這個lib ...然後得分這個答案。 – enguerran 2010-12-14 16:04:01

相關問題