【问题标题】:Unable to read data from the transport connection: C# HtmlAgilityPack无法从传输连接读取数据:C# HtmlAgilityPack
【发布时间】:2013-08-25 22:43:46
【问题描述】:

所以我正在用 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 行,我正在 forloop 中加载页面:

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

我已经尝试对错误进行一些研究,但没有很多 在那里形成。

【问题讨论】:

  • 这与 Html Agility Pack 库无关。错误来自 HTTP/TCP/Socket 层。这只是意味着服务器有问题或只是拒绝您的呼叫。
  • 好的,谢谢你的回答,但是我该如何解决这个错误呢?
  • 它可能是由很多事情引起的。如果您不拥有服务器,则无法真正知道。例如,他们可能会将您检测为黑客。

标签: c# web-scraping html-agility-pack inner-exception


【解决方案1】:

我在下载了 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多