Retry HTTP request from .NET with different proxy server

最后都变了- 提交于 2021-01-29 13:25:38

问题


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.


回答1:


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"));
    });
}



回答2:


For my use case, it turns out that I was better off without Polly.

public static string RequestWithProxies(string url, string[] proxies)
{
    var client = new WebClient
    {
        Credentials = new NetworkCredential(username, password)
    };
    var result = String.Empty;

    foreach (var proxy in proxies)
    {
        client.Proxy = new WebProxy(proxy);
        try
        {
            result = client.DownloadString(new Uri(url));
        }
        catch (Exception) { if (!String.IsNullOrEmpty(result)) break; }
    }

    if (String.IsNullOrEmpty(result)) throw new Exception($"Exhausted proxies: {String.Join(", ", proxies)}");
    return result;
}



回答3:


Here is an answer with Polly, but I don't like it as much as the answer without Polly or Polly with wait to retry.

public static string RequestWithProxies(string url, string[] proxies)
{
    var client = new WebClient { Credentials = new NetworkCredential(username, password) };
    var result = String.Empty;
    var proxyIndex = 0;

    var policy = Policy.Handle<Exception>()
        .Retry(
            retryCount: proxies.Length,
            onRetry: (exception, _) => proxyIndex++);

    policy.Execute(() =>
    {
        if (proxyIndex >= proxies.Length) throw new Exception($"Exhausted proxies: {String.Join(", ", proxies)}");

        client.Proxy = new WebProxy(proxies[proxyIndex]);
        result = client.DownloadString(new Uri(url));
    });

    return result;
}


来源:https://stackoverflow.com/questions/51201899/retry-http-request-from-net-with-different-proxy-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!