2013-08-25 118 views
0

因此,我正在用C#中的HtmlAgilityPack製作一個程序(用於自己的目的),以便在某個點加載一個網頁。加載大量的頁面後 ,我得到這個錯誤:無法從傳輸連接讀取數據:C#HtmlAgilityPack

Unhandled Exception: System.IO.IOException: Unable to read data from the transpo 
rt connection: An existing connection was forcibly closed by the remote host. -- 
-> System.Net.Sockets.SocketException: An existing connection was forcibly close 
d by the remote host 
    at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, 
SocketFlags socketFlags) 
    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 s 
ize) 
    --- End of inner exception stack trace --- 
    at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) 
    at System.IO.StreamReader.ReadBuffer() 
    at System.IO.StreamReader.ReadToEnd() 
    at HtmlAgilityPack.HtmlDocument.Load(TextReader reader) in d:\Source\htmlagil 
itypack.new\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 612 
    at HtmlAgilityPack.HtmlWeb.Get(Uri uri, String method, String path, HtmlDocum 
ent doc, IWebProxy proxy, ICredentials creds) in d:\Source\htmlagilitypack.new\T 
runk\HtmlAgilityPack\HtmlWeb.cs:line 1422 
    at HtmlAgilityPack.HtmlWeb.LoadUrl(Uri uri, String method, WebProxy proxy, Ne 
tworkCredential creds) in d:\Source\htmlagilitypack.new\Trunk\HtmlAgilityPack\Ht 
mlWeb.cs:line 1479 
    at HtmlAgilityPack.HtmlWeb.Load(String url, String method) in d:\Source\htmla 
gilitypack.new\Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1103 
    at HtmlAgilityPack.HtmlWeb.Load(String url) in d:\Source\htmlagilitypack.new\ 
Trunk\HtmlAgilityPack\HtmlWeb.cs:line 1061 
    at ConsoleApplication1.Program.Main(String[] args) in 
c:\Users\...ConsoleApplication1\Program.c 
s:line 37 

在第37行我加載一個for循環內頁:

for (var i = 0; i< 5000; i++) 
    var page = web.Load(url+Convert.ToString(i+1)+"/"); 

我試圖做一些研究上的錯誤,但是在那裏沒有太多的組建。

+2

這與Html Agility Pack庫無關。錯誤來自HTTP/TCP/Socket層。這只是意味着服務器有問題或者只是拒絕你的電話。 –

+0

好的,謝謝你的回答,但我該如何解決這個錯誤? – breght

+0

它可能由許多事情引起。如果你不擁有這臺服務器,你真的不知道。例如,他們可能會將您檢測爲黑客。 –

回答

0

我下載了1000多個網頁後出現同樣的錯誤。在循環中用一個額外的IOException解決它。 這是我的代碼:

HtmlWeb web = new HtmlWeb(); 
web.PreRequest = delegate(HttpWebRequest webRequest) 
{ 
    webRequest.Timeout = 15000; 
    return true; 
}; 

try { doc = web.Load(yUrl); } 
catch (WebException ex) 
{ 
    reTryCounter++; 
    if (reTryCounter == 19) { MessageBox.Show("Error Program 1121 , Download webpage \n" + ex.ToString()); } 
} 
catch (IOException ex2) 
{ 
    MessageBox.Show("Error Program 1125 , IOException Download webpage \n" + ex2.ToString()); 
    return null; 
} 
相關問題