2011-01-08 49 views
32

當我從一個網址提取數據與403響應HttpURLConnection類的403錯誤閱讀響應內容

is = conn.getInputStream(); 

它拋出IOException,我無法獲得響應數據。

但是當我使用Firefox和直接訪問URL時,ResponseCode仍然是403,但我可以得到的HTML內容

回答

53

HttpURLConnection.getErrorStream方法將返回可用於檢索從錯誤狀況數據的InputStream(如404),根據javadocs。

+3

不,它不會,因爲函數的代碼只包含'return null;'線。 (Java 6,7) – Gangnus 2014-03-25 13:56:38

+2

@Gangnus仔細閱讀Javadoc:「如果連接沒有連接,或者如果服務器在連接時沒有錯誤,或者如果服務器發生錯誤但沒有發送錯誤數據,則此方法將返回null,這是默認值。「 否則(錯誤4xx),您將獲得要讀取的流。 – 2014-06-05 09:20:32

+0

@MiljenMikic代碼和Javadoc之間的區別只意味着最後一個是錯誤的。 – Gangnus 2014-06-05 11:43:56

10

嘗試這樣:

try { 
    String text = "url"; 
    URL url = new URL(text); 
    URLConnection conn = url.openConnection(); 
    // fake request coming from browser 
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;  rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); 
    String f = in.readLine(); 
    in.close(); 
    System.out.println(f); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
16

HttpURLConnection用例:

String response = null; 
try { 
    URL url = new URL("http://google.com/pagedoesnotexist"); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

    // Hack to force HttpURLConnection to run the request 
    // Otherwise getErrorStream always returns null 
    connection.getResponseCode(); 
    InputStream stream = connection.getErrorStream(); 
    if (stream == null) { 
     stream = connection.getInputStream(); 
    } 
    // This is a try with resources, Java 7+ only 
    // If you use Java 6 or less, use a finally block instead 
    try (Scanner scanner = new Scanner(stream)) { 
     scanner.useDelimiter("\\Z"); 
     response = scanner.next(); 
    } 
} catch (MalformedURLException e) { 
    // Replace this with your exception handling 
    e.printStackTrace(); 
} catch (IOException e) { 
    // Replace this with your exception handling 
    e.printStackTrace(); 
} 
0

即使在添加代理程序字符串後,我也收到了相同的錯誤。最後經過數天調查發現問題。如果url方案以「HTTPS」開頭,會導致錯誤403,這真的很糟糕。它應該是小寫(「https」)。因此,請確保在打開連接之前調用「url.toLowercase()」。