Is there an elegant way to do it? I have too many request being passed in a specific time that throwing a 503 (service unavailable) exception. Thanks
protected void CallApi(string uriString)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsync(uriString, new StringContent("{ Data to be posted }")).Result;
for (int i = 0; i < MaxRetries; i++)
{
if (response.IsSuccessStatusCode)
{
break;
}
else
{
Thread.Sleep(TimeSpan.FromMinutes(1));
response = client.PostAsync(uriString, new StringContent("{ Data to be posted }")).Result;
}
}
throw new Exception("status : " + (int)response.StatusCode + ", Content :" + response.Content.ReadAsStringAsync().Result);
}
}