I've made a c# app which can control Onvif cameras which it works to an extent. I can discover, connect and control the cameras, however after sending a few (different amount each time) movement commands the camera becomes unresponsive, the error "The operation has timed out" appears and I have to physically restart my camera to use it again. The code is as follows:
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;
}
}
The code hangs on the var response line. Using wireshark I see that there is no response coming back from the camera. Is there a better method I can use to send the requests or a way that I can ignore the responses?