簡體   English   中英

java.io.IOException:服務器返回HTTP響應代碼:503的URL:錯誤

[英]java.io.IOException: Server returned HTTP response code: 503 for URL: Error

我通過從網站獲取HTML代碼然后從Java中解析數據來從網站上抓取數據。

我目前正在使用java.net.URL和java.net.URLConnection。 這是我用來從某個網站獲取HTML代碼的代碼(在此網站上找到,經過略微修改以滿足我的需要):

public static String getURL(String name) throws Exception{

    //Set URL
    String s = "";
    URL url = new URL(name);
    URLConnection spoof = url.openConnection();

    //Spoof the connection so we look like a web browser
    spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
    BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
    String strLine = "";

    //Loop through every line in the source
    while ((strLine = in.readLine()) != null){

        //Prints each line to the console
        s = s + strLine + "\n";
    }
    return s;
}

當我運行它時,大約100-200個網頁可以正確接收HTML代碼。 但是,在抓取HTML代碼之前,我得到了“ java.io.IOException:服務器返回的HTTP響應代碼:URL 503”異常。 我已經充分研究這個話題,像其他的問題, 這樣一個並不包括我現在用的包。

先謝謝您的幫助!

服務器可能有限制。 在這種情況下,您可以嘗試使用Socket和input / outputStream代替URLConnection

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM