I'm configuring IP cameras with c# .NET. I'm applying a new firmware which reboots the camera. Post-reboot, the camera actively rejects connections for a time, then goes to 503-service unavailable for a bit more time.
I'm looking for a method that will continually probe the camera, ignoring AggregateExceptions and 503's until it returns 200/OK. Then continue with the configuration process.
This is the code I have thus far, and it catches the exceptions, but again I want it to keep trying until successful.
var statusDic = new Dictionary<HttpStatusCode, string>();
try
{
using (var client = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip,
Credentials = new NetworkCredential("root", "pass")
}))
{
client.BaseAddress = new Uri($@"http://{dicPair.Value}/axis-cgi/param.cgi");
var response = client.GetAsync(Source.config);
while (response.Exception != null || response.Result.StatusCode != HttpStatusCode.OK)
{
Thread.Sleep(5000);
response = client.GetAsync(Source.config);
}
statusDic.Add(response.Result.StatusCode, response.Result.ToString());
}
}
catch (WebException ex)
{
using (var sr =
new StreamReader(ex.Response.GetResponseStream() ?? throw new InvalidOperationException()))
{
Logger.LogError(sr.ReadToEnd());
}
}
catch (AggregateException ex)
{
Logger.LogError(ex.Message);
}
return statusDic;