【发布时间】:2014-02-26 17:28:21
【问题描述】:
您好,我正在为一个网站制作爬虫。爬了大约 3 个小时后,我的应用程序在 WebException 上停止了。下面是我在 c# 中的代码。 client 是预定义的 WebClient 对象,每次处理完 gameDoc 时都会释放该对象。 gameDoc 是一个HtmlDocument 对象(来自HtmlAgilityPack)
while (retrygamedoc)
{
try
{
gameDoc.LoadHtml(client.DownloadString(url)); // this line caused the exception
retrygamedoc = false;
}
catch
{
client.Dispose();
client = new WebClient();
retrygamedoc = true;
Thread.Sleep(500);
}
}
我尝试使用来自this 答案的以下代码(以保持网络客户端新鲜)
while (retrygamedoc)
{
try
{
using (WebClient client2 = new WebClient())
{
gameDoc.LoadHtml(client2.DownloadString(url)); // this line cause the exception
retrygamedoc = false;
}
}
catch
{
retrygamedoc = true;
Thread.Sleep(500);
}
}
但结果还是一样。然后我使用 StreamReader,结果保持不变!下面是我使用 StreamReader 的代码。
while (retrygamedoc)
{
try
{
// using native to check the result
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(url);
string responsestring = string.Empty;
HttpWebResponse response = (HttpWebResponse)webreq.GetResponse(); // this cause the exception
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responsestring = reader.ReadToEnd();
}
gameDoc.LoadHtml(client.DownloadString(url));
retrygamedoc = false;
}
catch
{
retrygamedoc = true;
Thread.Sleep(500);
}
}
我应该怎么做和检查?我很困惑,因为我能够在同一站点上的某些页面上爬行,然后在大约 1000 个结果中,它导致了异常。来自异常的消息仅为The request was aborted: The connection was closed unexpectedly.,状态为ConnectionClosed
PS。该应用是桌面表单应用。
更新:
现在我正在跳过这些值并将它们设置为 null,以便继续进行爬取。但是如果真的需要数据,我还是得手动更新爬取结果,因为结果包含数千条记录,这很累。请帮帮我。
示例:
就像您从网站下载了大约 1300 条数据,然后应用程序停止说 The request was aborted: The connection was closed unexpectedly.,而您的所有互联网连接仍然保持正常且速度良好。
【问题讨论】:
标签: c# webclient system.net.webexception