使用C#将XML发布到IP摄像机

时间:2015-02-03 15:17:59

标签: c# http-headers

我有一台数码网络摄像机。预设使用设置静态IP地址,我已经询问制造商是否有可以调用的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();
        }

但是不起作用。我是否犯了明显的错误?

1 个答案:

答案 0 :(得分:2)

试试这个。 如果您使用的是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());
    }