带有代理的HttpClient / HttpClientHandler上的超时

时间:2019-09-24 18:35:23

标签: c# http webclient dotnet-httpclient

我正在抓捕一个需要使用数百/数千代理才能通过的站点。我有20,000个代理的列表,但是很多已经死了。当我遇到无效代理时,我会对其进行标记,然后使用其他代理进行重试,但是对于无效代理,超时通常约为15秒。

我尝试设置Timeout属性,但这似乎只影响它连接到代理服务器后的实际HTTP请求。当我查看了数十页时,没有看到我处理超时的代理,只是HttpClient请求。

如何设置一个使代理更快超时的请求,以便我可以继续下一个?

foreach(var proxy in proxies){
        var prox = new WebProxy($"{proxy.ip}:{proxy.port}");
        var clientHandler = new HttpClientHandler { Proxy = prox, UseProxy = true };
        var client = new HttpClient(clientHandler);
        try{
        var res = await client.GetStringAsync("http://example.com");
            //success, do something
            break;
        }catch{
            //failure, mark and try again
        }

}

2 个答案:

答案 0 :(得分:0)

使用秒表的示例:

var timer = Stopwatch.StartNew();
try
{
    var resultTask = client.GetStringAsync("http://example.com");
    while (timer.Elapsed.TotalSeconds <= 2 && !resultTask.IsCompleted)
        await Task.Delay(100);

    if (!resultTask.IsCompleted)
        throw new Exception();

    string reply = resultTask.Result;
}
catch
{
    throw;
}
finally
{
    timer.Stop();
}

答案 1 :(得分:0)

尝试使用CancellationTokenSource的这种方法:

var source = new CancellationTokenSource();
source.CancelAfter(TimeSpan.FromSeconds(5));
var prox = new WebProxy($"{proxy.ip}:{proxy.port}");
var clientHandler = new HttpClientHandler { Proxy = prox, UseProxy = true };
var client = new HttpClient(clientHandler);
try
{
    var res = await client.GetStringAsync("http://example.com", source.Token);
    //success, do something
    break;
}
catch
{
    //failure or TaskCanceledException
}