2013-01-19 74 views
1

我使用HttpURLConnection連接到網站並收到ResponseCode=404 (HTTP_NOT_FOUND)。不過,我在瀏覽器(IE)中打開網站沒有問題。HTTPURLConnection.getContent java.io.FileNotFoundException

爲什麼不同,我能做些什麼呢?

這是我的計劃:

String responseMsg = ""; 

String cgsUrl = "http://localhost:9081/ntes/"; 

URL url = new URL(cgsUrl); 
System.out.println("ouuuuuuu-->"+url.getContent()); 

InputStream in = url.openConnection().getInputStream(); 

StringBuffer respDataBuf = new StringBuffer(); 
respDataBuf.setLength(0); 
int b = -1; 

while((b = in.read()) != -1) { 
    respDataBuf.append((char)b); 
} 
responseMsg = respDataBuf.toString(); 
+0

你在服務器端使用servlet嗎?它是否接受get和post請求? –

+1

您能告訴我們錯誤堆棧跟蹤嗎? –

+1

它沒有System.out.println(「ouuuuuuu - >」+ url.getContent()); ? –

回答

1

如果這是一個404錯誤,這無疑是一個特定的服務器配置。

也許你的用戶代理被禁止,或者你沒有攜帶特殊的頭文件等等。我建議你從瀏覽器中複製頭文件(所有這些頭文件)並使用它們在Java程序中發出請求。

然後你逐個扔掉它們以找到必需的一個