【发布时间】:2018-08-07 14:40:53
【问题描述】:
我正在使用 c# .NET 配置 IP 摄像机。我正在应用重新启动相机的新固件。重启后,相机主动拒绝连接一段时间,然后进入 503 服务不可用一段时间。
我正在寻找一种可以持续探测相机的方法,忽略 AggregateExceptions 和 503,直到它返回 200/OK。然后继续配置过程。
这是我到目前为止的代码,它会捕获异常,但我希望它继续尝试直到成功。
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;
【问题讨论】:
-
添加一个
while和一个布尔值.. -
我可能会使用您的代码创建一个计时器作为经过的事件。也许间隔 5 秒?
-
像拒绝服务攻击?或者比这更慢,比如一个带有
Task.Delay()的循环来节流?
标签: c# .net winforms dotnet-httpclient