Post XML 到使用 C# 的网络摄像机
Post XML to a IP Camera using C#
我有一台数码网络摄像机。预设使用set static IP address,问过厂家有没有API我可以打电话设置成DHCP。
他们回复:
PUT /Network/interfaces/1/ipAddress HTTP/1.1
Authorization: Basic YWRtaW46MTIzNDU=
Content-Type:text/xml
Content-Length:387
<IPAddress version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
<ipVersion>v4</ipVersion>
<addressingType>dynamic</addressingType>
<ipAddress>172.2.62.49</ipAddress>
<subnetMask>255.255.255.0</subnetMask>
<DefaultGateway>
<ipAddress>172.2.62.1</ipAddress>
</DefaultGateway>
<PrimaryDNS>
<ipAddress>0.0.0.0</ipAddress>
</PrimaryDNS>
</IPAddress>
所以,我将其翻译为:
private void button1_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest req = null;
WebResponse rsp = null;
string uri = "http://192.0.0.64/Network/interfaces/1/ipAddress";
req =(HttpWebRequest) WebRequest.Create(uri);
req.UseDefaultCredentials = true;
//req.Credentials = new NetworkCredential("admin", "12345"); //I have tried using this as well as these are the default admin/pw supplied
req.Method = "PUT";
req.ProtocolVersion = HttpVersion.Version11;
req.ContentLength = 387;
string _cred = string.Format("{0} {1}", "Basic", "YWRtaW46MTIzNDU=");
req.Headers[HttpRequestHeader.Authorization] = _cred;
req.ContentType = "text/xml";
StreamWriter writer = new StreamWriter(req.GetRequestStream());
writer.WriteLine(GetDHCPPost());
writer.Close();
rsp = req.GetResponse();
}
catch (Exception ex)
{
//errors here >> cannot connect to server
}
}
private string GetDHCPPost()
{
StringBuilder sb = new StringBuilder();
sb.Append("<IPAddress version=\"1.0\" xmlns=\"http://www.hikvision.com/ver10/XMLSchema\">");
sb.Append("<ipVersion>v4</ipVersion>");
sb.Append("<addressingType>dynamic</addressingType>");
sb.Append("<ipAddress>172.2.62.49</ipAddress>");
sb.Append("<subnetMask>255.255.255.0</subnetMask>");
sb.Append("<DefaultGateway>");
sb.Append("<ipAddress>172.2.62.1</ipAddress>");
sb.Append("</DefaultGateway>");
sb.Append("<PrimaryDNS>");
sb.Append("<ipAddress>0.0.0.0</ipAddress>");
sb.Append("</PrimaryDNS>");
sb.Append("</IPAddress> ");
return sb.ToString();
}
但是不起作用。我犯了一个明显的错误吗?
试试这个。
如果您使用 admin/12345。
我这样做是为了将温度叠加层写入我的海康威视相机。
您的 xml 代码绝对正确。我不是 100% 需要声明端口号,我知道我一直都有。
SendToHikVision("admin", "12345", "192.0.0.64", "*The Port You're Using");
void SendToHikVision(string UserName, string Password, string IP, string Port)
{
try
{
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://" + IP + ":" + Port + "/Network/interfaces/1/");
wr.Accept = "*/*";
wr.Method = "PUT";
wr.ReadWriteTimeout = 5000;
wr.Credentials = new NetworkCredential(UserName, Password);
byte[] pBytes = GetDHCPPost();
wr.ContentLength = pBytes.Length;
using (Stream DS = wr.GetRequestStream())
{
DS.Write(pBytes, 0, pBytes.Length);
DS.Close();
}
wr.BeginGetResponse(r => { var reponse = wr.EndGetResponse(r); }, null);
}
catch { }
}
byte[] GetDHCPPost()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<IPAddress version=\"1.0\" xmlns=\"http://www.hikvision.com/ver10/XMLSchema\">");
sb.AppendLine("<ipVersion>v4</ipVersion>");
sb.AppendLine("<addressingType>dynamic</addressingType>");
sb.AppendLine("<ipAddress>172.2.62.49</ipAddress>");
sb.AppendLine("<subnetMask>255.255.255.0</subnetMask>");
sb.AppendLine("<DefaultGateway>");
sb.AppendLine("<ipAddress>172.2.62.1</ipAddress>");
sb.AppendLine("</DefaultGateway>");
sb.AppendLine("<PrimaryDNS>");
sb.AppendLine("<ipAddress>0.0.0.0</ipAddress>");
sb.AppendLine("</PrimaryDNS>");
sb.AppendLine("</IPAddress> ");
return Encoding.ASCII.GetBytes(sb.ToString());
}
我有一台数码网络摄像机。预设使用set static IP address,问过厂家有没有API我可以打电话设置成DHCP。
他们回复:
PUT /Network/interfaces/1/ipAddress HTTP/1.1
Authorization: Basic YWRtaW46MTIzNDU=
Content-Type:text/xml
Content-Length:387
<IPAddress version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema">
<ipVersion>v4</ipVersion>
<addressingType>dynamic</addressingType>
<ipAddress>172.2.62.49</ipAddress>
<subnetMask>255.255.255.0</subnetMask>
<DefaultGateway>
<ipAddress>172.2.62.1</ipAddress>
</DefaultGateway>
<PrimaryDNS>
<ipAddress>0.0.0.0</ipAddress>
</PrimaryDNS>
</IPAddress>
所以,我将其翻译为:
private void button1_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest req = null;
WebResponse rsp = null;
string uri = "http://192.0.0.64/Network/interfaces/1/ipAddress";
req =(HttpWebRequest) WebRequest.Create(uri);
req.UseDefaultCredentials = true;
//req.Credentials = new NetworkCredential("admin", "12345"); //I have tried using this as well as these are the default admin/pw supplied
req.Method = "PUT";
req.ProtocolVersion = HttpVersion.Version11;
req.ContentLength = 387;
string _cred = string.Format("{0} {1}", "Basic", "YWRtaW46MTIzNDU=");
req.Headers[HttpRequestHeader.Authorization] = _cred;
req.ContentType = "text/xml";
StreamWriter writer = new StreamWriter(req.GetRequestStream());
writer.WriteLine(GetDHCPPost());
writer.Close();
rsp = req.GetResponse();
}
catch (Exception ex)
{
//errors here >> cannot connect to server
}
}
private string GetDHCPPost()
{
StringBuilder sb = new StringBuilder();
sb.Append("<IPAddress version=\"1.0\" xmlns=\"http://www.hikvision.com/ver10/XMLSchema\">");
sb.Append("<ipVersion>v4</ipVersion>");
sb.Append("<addressingType>dynamic</addressingType>");
sb.Append("<ipAddress>172.2.62.49</ipAddress>");
sb.Append("<subnetMask>255.255.255.0</subnetMask>");
sb.Append("<DefaultGateway>");
sb.Append("<ipAddress>172.2.62.1</ipAddress>");
sb.Append("</DefaultGateway>");
sb.Append("<PrimaryDNS>");
sb.Append("<ipAddress>0.0.0.0</ipAddress>");
sb.Append("</PrimaryDNS>");
sb.Append("</IPAddress> ");
return sb.ToString();
}
但是不起作用。我犯了一个明显的错误吗?
试试这个。 如果您使用 admin/12345。 我这样做是为了将温度叠加层写入我的海康威视相机。 您的 xml 代码绝对正确。我不是 100% 需要声明端口号,我知道我一直都有。
SendToHikVision("admin", "12345", "192.0.0.64", "*The Port You're Using");
void SendToHikVision(string UserName, string Password, string IP, string Port)
{
try
{
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://" + IP + ":" + Port + "/Network/interfaces/1/");
wr.Accept = "*/*";
wr.Method = "PUT";
wr.ReadWriteTimeout = 5000;
wr.Credentials = new NetworkCredential(UserName, Password);
byte[] pBytes = GetDHCPPost();
wr.ContentLength = pBytes.Length;
using (Stream DS = wr.GetRequestStream())
{
DS.Write(pBytes, 0, pBytes.Length);
DS.Close();
}
wr.BeginGetResponse(r => { var reponse = wr.EndGetResponse(r); }, null);
}
catch { }
}
byte[] GetDHCPPost()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<IPAddress version=\"1.0\" xmlns=\"http://www.hikvision.com/ver10/XMLSchema\">");
sb.AppendLine("<ipVersion>v4</ipVersion>");
sb.AppendLine("<addressingType>dynamic</addressingType>");
sb.AppendLine("<ipAddress>172.2.62.49</ipAddress>");
sb.AppendLine("<subnetMask>255.255.255.0</subnetMask>");
sb.AppendLine("<DefaultGateway>");
sb.AppendLine("<ipAddress>172.2.62.1</ipAddress>");
sb.AppendLine("</DefaultGateway>");
sb.AppendLine("<PrimaryDNS>");
sb.AppendLine("<ipAddress>0.0.0.0</ipAddress>");
sb.AppendLine("</PrimaryDNS>");
sb.AppendLine("</IPAddress> ");
return Encoding.ASCII.GetBytes(sb.ToString());
}