title: sending Tr064 Requests to AVM FritzBox 7270
body: <p>I am just learning how to send SOAP requests to my AVM FritzBox 7270 router using C#.</p>
  
  <p>Here ist my method that sends a SOAP request to the router:</p>
  
  <pre><code>    private string Execute(string controlUrl, string serviceType, string action)
      {
          WebRequest webRequest = WebRequest.Create("http://fritz.box:49000" + controlUrl);
          HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
          httpRequest.Method = "POST";
          httpRequest.ContentType = "text/xml; charset=utf-8";
          httpRequest.Headers.Add("SOAPACTION", string.Format("{0}#{1}", serviceType, action));
          httpRequest.ProtocolVersion = HttpVersion.Version11;
          httpRequest.Credentials = new NetworkCredential("username", "password"); 
          Stream requestStream = httpRequest.GetRequestStream();
  
          StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
  
          streamWriter.Write(GetBody(serviceType, action));
          streamWriter.Close();
          //Get the Response    
          try
          {
              HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
              StreamReader srd = new StreamReader(wr.GetResponseStream());
              return srd.ReadToEnd();
          }
          catch (WebException)
          {
              return null;
          }
      }
  </code></pre>
  
  <p>Function GetBody:</p>
  
  <pre><code>    private string GetBody(string serviceType, string action)
      {
          const string fmt = @"&lt;?xml version=""1.0"" encoding=""utf-8""?&gt;
  &lt;s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"" s:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""&gt;
    &lt;s:Body&gt;
      &lt;u:{0} xmlns:u=""{1}"" /&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  ";        
              return string.Format(fmt, action, serviceType);
          }
      }
  </code></pre>
  
  <p>I tested this method with parameters that I found anywhere in the internet:</p>
  
  <pre><code>controlUrl = "/upnp/control/WANIPConn1"
  serviceType = "urn:schemas-upnp-org:service:WANIPConnection:1"
  action = "GetExternalIPAddress"
  </code></pre>
  
  <p>This works well.</p>
  
  <p>However, I think I should go the official way and first send a request</p>
  
  <pre><code>http://fritz.box:49000/tr64desc.xml
  </code></pre>
  
  <p>to receive parameters valid for my actual router. In the response to this request I find the following node:</p>
  
  <pre><code>&lt;service&gt;
    &lt;serviceType&gt;urn:dslforum-org:service:WANIPConnection:1&lt;/serviceType&gt;
    &lt;serviceId&gt;urn:WANIPConnection-com:serviceId:WANIPConnection1&lt;/serviceId&gt;
    &lt;controlURL&gt;/upnp/control/wanipconnection1&lt;/controlURL&gt;
    &lt;eventSubURL&gt;/upnp/control/wanipconnection1&lt;/eventSubURL&gt;
    &lt;SCPDURL&gt;/wanipconnSCPD.xml&lt;/SCPDURL&gt;
  &lt;/service&gt;
  </code></pre>
  
  <p>Using these values for serviceId and controlUrl, I get error 500 (Internal Server Error).</p>
  
  <p>Who can help me? What is wrong in my code?</p>