我通过继承本机类并覆盖该方法创建了一个WebClient
具有更长属性的新类:Request.Timeout
WebClient
GetWebRequest
public class LongerTimeoutWebClient : WebClient
{
public LongerTimeoutWebClient()
{
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest r = base.GetWebRequest(address);
r.Timeout = 30 * 1000; //30 second timeout
return r;
}
}
我使用这个新WebClient
类来并行调用多个网页:
Parallel.ForEach(links, new ParallelOptions { MaxDegreeOfParallelism = 2 }, link =>
{
byte[] requestBytes;
var stopwatch = new System.Diagnostics.Stopwatch();
using (var client = new LongerTimeoutWebClient())
{
try
{
ServicePointManager.Expect100Continue = false;
stopwatch.Start();
requestBytes = client.DownloadData(link);
}
catch (Exception ex)
{
stopwatch.Stop();
Console.WriteLine("Could not get: " + link + ". Timeout after: " + stopwatch.Elapsed.TotalSeconds + "s");
Console.WriteLine(ex);
return;
}
}
//More code here
}
但是,我间歇性地遇到408 Request Timeout
错误。发生超时错误时的秒表值在 20 秒左右。
我还检查了新类的方法使用的对象的Timeout
属性,它设置为 30000 (ms)。Request
GetWebResponse
WebClient
我已将 设置MaxDegreeOfParallelism
为 1,但仍然出现错误,因此这可能意味着并行化不是问题的原因。
为什么我Request
的超时时间是 20 秒而不是 30 秒?