2014-02-14 28 views
0

解析網頁時,我得到鏈接href = http://www.onvista.de/aktien/snapshot.html?ID_OSI=36714349 在瀏覽器中發佈此鏈接時,它將其替換爲「http://www.onvista.de/aktien/Adidas-Aktie-DE000A1EWWW0」並正確呈現。 但與Java我無法檢索頁面。我使用了此處建議的以下示例來顯示重定向的URL。如何通過java代碼獲取被替換/重定向的URL

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 

public class GetRedirected { 

    public GetRedirected() throws MalformedURLException, IOException { 
     String url="http://www.onvista.de/aktien/snapshot.html?ID_OSI=36714349"; 
     URLConnection con = new URL(url).openConnection(); 
     System.out.println("orignal url: " + con.getURL()); 
     con.connect(); 
     System.out.println("connected url: " + con.getURL()); 
     InputStream is = con.getInputStream(); 
     System.out.println("redirected url: " + con.getURL()); 
     is.close(); 
    } 

    public static void main(String[] args) throws Exception { 
     new GetRedirected(); 
    } 
} 

但是它在失敗的「InputStream is =」 - 語句與附加的錯誤消息。我該如何解決這個問題。任何想法是受歡迎的。

一部開拓創新網址:www.onvista.de/aktien/snapshot.html?ID_OSI=36714349

連接網址:www.onvista.de/aktien/snapshot.html?ID_OSI=36714349

異常在線程 「主要」 java.io.IOException異常:服務器返回的HTTP

響應代碼:403網址:www.onvista.de/aktien/snapshot.html?ID_OSI=36714349

在sun.net。 www.protocol.http.HttpURLConnection.getIn putStream(未知來源)

在de.gombers.broker ....

回答

0

非常常見的錯誤:當HttpURLConnection的響應的HTTP狀態代碼表示錯誤(AFAIK> = 400),訪問getInputStream()引發異常。您必須檢查getResponseCode(),然後決定是否必須致電getInputStream()getErrorStream()。因此,您應該先撥打getResponseCode(),而不是致電getInputStream()

但實際上,我無法重現你的錯誤,對我來說它的工作(雖然我使用一個名爲DavidWebb一個微小的抽象庫:

public void testAktienAdidas() throws Exception { 

    Webb webb = Webb.create(); 
    Response<String> response = webb 
      .get("http://www.onvista.de/aktien/snapshot.html?ID_OSI=36714349") 
      .asString(); 

    assertEquals(200, response.getStatusCode()); 
    assertNotNull(response.getBody()); 
    assertTrue(response.getBody().contains("<!DOCTYPE html>")); 
} 

我沒有得到一個重定向,大概這是通過在客戶端完成不支持JavaScript或有一些,其評價的HTTP報頭狀User-Agent服務器端邏輯

但是,如果您遇到重定向,你可以告訴HttpURLConnection to automatically follow them

conn.setInstanceFollowRedirects(true); 
+0

你說得對。在開始行動之前,應該先測試環境是否允許。但顯示的代碼已放在一起,以顯示問題。但無論如何,這個問題現在已經解決了。必須使用合適的用戶代理來調用websever。爲此目的指定「Mozilla/5.0(Windows NT 6.3; WOW64; rv:26.0)Gecko/20100101 Firefox/26.0」這個技巧。 – Ulrich

0
you can get retrieve it by this code 
package Test; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class HttpRedirectExample { 

    public static void main(String[] args) { 

    try { 

    String url = "http://www.onvista.de/aktien/snapshot.html?ID_OSI=36714349"; 
// String urlTest="https://api.twitter.com/oauth/authenticate"; 

URL obj = new URL(url); 
    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"); 

    System.out.println("Request URL ... " + url); 

    boolean redirect = false; 


    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; 
    } 

    System.out.println("Response Code ... " + status); 

    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 = (HttpURLConnection) new URL(newUrl).openConnection(); 
     conn.setRequestProperty("Cookie", cookies); 
     conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); 
     conn.addRequestProperty("User-Agent", "Mozilla"); 
     conn.addRequestProperty("Referer", "google.com"); 

     System.out.println("Redirect to URL : " + newUrl); 

    } 

    BufferedReader in = new BufferedReader(
           new InputStreamReader(conn.getInputStream())); 
    String inputLine; 
    StringBuffer html = new StringBuffer(); 

    while ((inputLine = in.readLine()) != null) { 
     html.append(inputLine); 
    } 
    in.close(); 

    System.out.println("URL Content... \n" + html.toString()); 
    System.out.println("Done"); 

    } catch (Exception e) { 
    e.printStackTrace(); 
    } 

    } 

}