title: Post XML to a IP Camera using C#
body: <p>I have a digital IP camera. It is preset to use set static IP address, and I have asked the manufacturer whether they have an API I can call to set it to DHCP.</p>
  
  <p>They replied:</p>
  
  <pre><code>PUT /Network/interfaces/1/ipAddress HTTP/1.1
  Authorization: Basic YWRtaW46MTIzNDU=
  Content-Type:text/xml
  Content-Length:387
  
  &lt;IPAddress version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema"&gt;
  &lt;ipVersion&gt;v4&lt;/ipVersion&gt;
  &lt;addressingType&gt;dynamic&lt;/addressingType&gt;
  &lt;ipAddress&gt;172.2.62.49&lt;/ipAddress&gt;
  &lt;subnetMask&gt;255.255.255.0&lt;/subnetMask&gt;
  &lt;DefaultGateway&gt;
  &lt;ipAddress&gt;172.2.62.1&lt;/ipAddress&gt;
  &lt;/DefaultGateway&gt;
  &lt;PrimaryDNS&gt;
  &lt;ipAddress&gt;0.0.0.0&lt;/ipAddress&gt;
  &lt;/PrimaryDNS&gt;
  &lt;/IPAddress&gt; 
  </code></pre>
  
  <p>So, I translated that to:</p>
  
  <pre><code>    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 &gt;&gt; cannot connect to server
          }
      }
  
   private string GetDHCPPost()
          {
              StringBuilder sb = new StringBuilder();   
              sb.Append("&lt;IPAddress version=\"1.0\" xmlns=\"http://www.hikvision.com/ver10/XMLSchema\"&gt;");
              sb.Append("&lt;ipVersion&gt;v4&lt;/ipVersion&gt;");
              sb.Append("&lt;addressingType&gt;dynamic&lt;/addressingType&gt;");
              sb.Append("&lt;ipAddress&gt;172.2.62.49&lt;/ipAddress&gt;");
              sb.Append("&lt;subnetMask&gt;255.255.255.0&lt;/subnetMask&gt;");
              sb.Append("&lt;DefaultGateway&gt;");
              sb.Append("&lt;ipAddress&gt;172.2.62.1&lt;/ipAddress&gt;");
              sb.Append("&lt;/DefaultGateway&gt;");
              sb.Append("&lt;PrimaryDNS&gt;");
              sb.Append("&lt;ipAddress&gt;0.0.0.0&lt;/ipAddress&gt;");
              sb.Append("&lt;/PrimaryDNS&gt;");
              sb.Append("&lt;/IPAddress&gt; ");
              return sb.ToString();
          }
  </code></pre>
  
  <p>But does not work. Am I making an obvious error?</p>