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