我已经制作了一个c#应用程序,它可以控制Onvif相机,它在某种程度上起作用。我可以发现,连接和控制相机,但是在发送一些(每次不同的量)移动命令后,相机变得没有响应,出现错误“操作已经超时”,我必须重新启动相机再次使用它。 代码如下:
Uri uri = new Uri(String.Format("http://"
+ ipAddr + "/onvif/" + "{0}", Service));
HttpWebRequest request = (HttpWebRequest) WebRequest.Create((uri));
request.KeepAlive = false;
//WebRequest request = WebRequest.Create((uri));
request.Method = "POST";
byte[] b = Encoding.ASCII.GetBytes(PostData);
request.ContentLength = b.Length;
request.Proxy = null;
request.Timeout = 5000;
Stream stream = request.GetRequestStream();
//Send Message
XmlDocument recData = new XmlDocument();
try
{
lock (_lock)
{
using (stream = request.GetRequestStream())
{
if (stream.CanWrite)
{
stream.Flush();
stream.Write(b, 0, b.Length);
}
}
//Store response
var response = (HttpWebResponse) request.GetResponse();
if (response.GetResponseStream() != null)
{
string responsestring = new StreamReader(response.GetResponseStream()).ReadToEnd();
recData.LoadXml(responsestring);
Thread.Sleep(1);
}
Thread.Sleep(200);
stream.Flush();
IsBusy = false;
Rec = true;
}
}
代码挂起在var响应行上。使用wireshark我发现相机没有响应。 是否有更好的方法可用于发送请求或我可以忽略响应的方式?