2017-07-06 212 views
0

我想抓取URL的數據「http://www.gc-zb.com/index/index.html」 但是當我喜歡這個工作,我得到了錯誤:產生java.io.IOException:服務器返回的HTTP響應代碼:521網址:

public class InvitedBids { 

    public static void main(String[] args) throws IOException { 

     InputStream inputStream=null; 
     HttpURLConnection httpConn=null; 
     InputStreamReader inputStreamReader=null; 
     BufferedReader bufferedReader=null; 
     StringBuilder contentBuf=null; 
     String myURL="http://www.gc-zb.com/index/index.html"; 
     URL url= null; 
     try { 
      url = new URL(myURL); 
      System.out.println(url); 
      httpConn= (HttpURLConnection) url.openConnection(); 
      httpConn.setRequestMethod("GET"); 

      inputStream=httpConn.getInputStream(); //error occurs 
      inputStreamReader=new InputStreamReader(inputStream,"utf-8"); 
      bufferedReader=new BufferedReader(inputStreamReader); 
      String line=""; 
      contentBuf=new StringBuilder(); 
      while ((line = bufferedReader.readLine())!= null) { 
       contentBuf.append(line); 
      } 
      String buf=contentBuf.toString(); 
      System.out.println(buf); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }finally { 
      //close I/O and HTTP 
     } 

    } 
} 

控制檯說:

http://www.gc-zb.com/index/index.html 
java.io.IOException: Server returned HTTP response code: 521 for URL: http://www.gc-zb.com/index/index.html 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
    at com.feilong.bid.InvitedBids.main(InvitedBids.java:43) 

任何人都知道如何解決它。謝謝!

+0

521 Web服務器是向下 由來服務器拒絕了Cloudflare的連接。請參閱https://en.wikipedia.org/wiki/List_of_HTTP_status_codes – Luftbaum

回答

0

像Jon之前說過的,錯誤521意味着服務器關閉。所以不要擔心,等到服務器啓動。 cloudflare docs

順便說一句,如果你想抓取文件,我強烈建議你使用JSOUP來獲取你的數據。我們在我的公司使用它,而且它是rulz。

例如:

Document doc = Jsoup.connect("http://example.com/").get(); 
String title = doc.title(); 

文檔: https://jsoup.org/cookbook/input/load-document-from-url

0

推薦使用 org.apache.commons.httpclient.HttpClient
爲了解決這個問題

相關問題