I am just learning how to send SOAP requests to my AVM FritzBox 7270 router using C#.
Here ist my method that sends a SOAP request to the router:
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;
}
}
Function GetBody:
private string GetBody(string serviceType, string action)
{
const string fmt = @"<?xml version=""1.0"" encoding=""utf-8""?>
<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"" s:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">
<s:Body>
<u:{0} xmlns:u=""{1}"" />
</s:Body>
</s:Envelope>
";
return string.Format(fmt, action, serviceType);
}
}
I tested this method with parameters that I found anywhere in the internet:
controlUrl = "/upnp/control/WANIPConn1"
serviceType = "urn:schemas-upnp-org:service:WANIPConnection:1"
action = "GetExternalIPAddress"
This works well.
However, I think I should go the official way and first send a request
http://fritz.box:49000/tr64desc.xml
to receive parameters valid for my actual router. In the response to this request I find the following node:
<service>
<serviceType>urn:dslforum-org:service:WANIPConnection:1</serviceType>
<serviceId>urn:WANIPConnection-com:serviceId:WANIPConnection1</serviceId>
<controlURL>/upnp/control/wanipconnection1</controlURL>
<eventSubURL>/upnp/control/wanipconnection1</eventSubURL>
<SCPDURL>/wanipconnSCPD.xml</SCPDURL>
</service>
Using these values for serviceId and controlUrl, I get error 500 (Internal Server Error).
Who can help me? What is wrong in my code?
I think the problem is solved:
There seem to be a lot of undocumeted features of the Fritz.Box, and the pararmeters I found in the internet oviously are part of thease features.
With
I can call
without problems.