使用其他代理服务器重试来自.NET的HTTP请求 [英] Retry HTTP request from .NET with different proxy server

查看:53
本文介绍了使用其他代理服务器重试来自.NET的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过.NET应用程序中的代理发出HTTP请求.我可以使用许多代理服务器,有时一个或多个代理服务器会崩溃.如何让我的应用使用其他代理重试HTTP请求?我乐于接受任何建议,并且听说过有关Polly的更多信息,可增加弹性.

I can issue HTTP requests through a proxy in a .NET app. There are a number of proxy servers I can use and sometimes one or more will go down. How can I have my app retry the HTTP request using a different proxy? I am open to any suggestion and have heard good things about Polly for adding resiliency.

推荐答案

如果您要使用Polly,也许是这样的:

If you were to use Polly, maybe something like this:

public void CallGoogle()
{
    var proxyIndex = 0;

    var proxies = new List<IWebProxy>
    {
        new WebProxy("proxy1.test.com"),
        new WebProxy("proxy2.test.com"),
        new WebProxy("proxy3.test.com")
    };

    var policy = Policy
                 .Handle<Exception>()
                 .WaitAndRetry(new[]
                     {
                         TimeSpan.FromSeconds(1),
                         TimeSpan.FromSeconds(2),
                         TimeSpan.FromSeconds(3)
                     }, (exception, timeSpan) => proxyIndex++);

    var client = new WebClient();

    policy.Execute(() =>
    {
        client.Proxy = proxies[proxyIndex];
        client.DownloadData(new Uri("https://www.google.com"));
    });
}

这篇关于使用其他代理服务器重试来自.NET的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆