我正在嘗試編寫一個程序來讀取網站http://judgephilosophies.wikispaces.com的html源代碼。我寫了一些簡單的java代碼,讀取和輸出源代碼,但它只是輸出「null」。然而,這是件奇怪的事情 - 如果我在代碼中將「http://judgephilosophies.wikispaces.com」替換爲任何其他網站,它就可以正常工作。它似乎只針對wikispaces.com域中的網站,該程序不起作用,並且我完全糊塗了爲什麼。代碼如下。非常感謝幫助。用Java訪問網頁的奇怪問題
import java.io.*;
import java.net.*;
public class AccessWebExample
{
public static void main (String[] args) throws Exception
{
//Create reader to access html source code
URL url = new URL ("http://judgephilosophies.wikispaces.com/");
InputStreamReader isr = new InputStreamReader (url.openStream());
BufferedReader reader = new BufferedReader (isr);
//Read and print the text
do
{
System.out.println(reader.readLine());
}
while(reader.readLine() != null);
}
}
它是如何工作的? - 如果該網站是一個Ajax網站,那麼它將無法工作。您擁有的程序只會從網站獲取HTML。 – 2011-04-23 19:44:08
@Romain - 不,服務器重定向。請參閱下面的答案。 – 2011-04-23 19:51:37