问题
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