Answer the question
In order to leave comments, you need to log in
How to retry a webrequest on error (C#)?
req = (HttpWebRequest) WebRequest.Create(get_url);
req.Timeout = 1500;
resp = (HttpWebResponse) req.GetResponse();
sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("utf-8"));
content = sr.ReadToEnd();
sr.Close();
Answer the question
In order to leave comments, you need to log in
1. You just repeat requests in a cycle with some periodicity
2. Instead of WebRequest, it is better to use HttpClient
3. To avoid sawing bicycles - use Polly
Through recursion, as an option
async Task<string> SendRequestUntilSuccess(string url)
{
try
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return response.Content.ReadAsStringAsync();
}
catch(HttpRequestException e)
{
return SendRequestUntilSuccess(url);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question