我正在使用WebClient
下载html文件:
private static string PullHTML(string url)
{
string data = null;
if (url != null)
{
if (!WebCache.TryGetValue(url, out data))
{
//THIS CODE IS REACHED
try
{
using (QuickWebClient wc = new QuickWebClient())
{
data = wc.DownloadString(url); //Debugging ends here
}
}
catch (WebException e)
{
//THIS CODE IS NEVER REACHED
Console.WriteLine(e.Message);
data = e.Message;
}
//NEITHER IS THIS
WebCache.Add(url, data);
}
}
return data;
}
private class QuickWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = 5 * 60 * 1000;
return w;
}
}
这是在一个单独的线程中运行的(我为了调试目的而重载它,之前就开始了问题)。现在,这段代码通常会使正在运行的线程崩溃。线程基本上停止了。我无法调试它,因为调试器一到达DownloadString
行就会离开。它就像按下 F5 一样。没有异常被抛出。线程不会继续。发生了什么事?