Skip to content

Commit

Permalink
Add single retry on WebException 502 to download data.
Browse files Browse the repository at this point in the history
  • Loading branch information
samjudson committed Feb 12, 2016
1 parent 870d164 commit 3b6ac7c
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions FlickrNet/FlickrResponderSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,29 @@ private static string DownloadData(string method, string baseUrl, string data, s
{
using (WebClient client = new WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
client.Encoding = Encoding.UTF8;
if (!string.IsNullOrEmpty(contentType)) client.Headers.Add("Content-Type", contentType);
if (!string.IsNullOrEmpty(authHeader)) client.Headers.Add("Authorization", authHeader);

if (method == "POST")
return client.UploadString(baseUrl, data);
else
return client.DownloadString(baseUrl);
Func<string> f = () => method == "POST" ? client.UploadString(baseUrl, data) : client.DownloadString(baseUrl);

try
{
return f();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
var response = ex.Response as HttpWebResponse;
if (response != null && response.StatusCode == HttpStatusCode.BadGateway)
{
System.Threading.Thread.Sleep(1000);
return f();
}
}
throw;
}
}
}
#else
Expand Down

0 comments on commit 3b6ac7c

Please sign in to comment.