C# 向AVM FritzBox 7270发送Tr064请求

C# 向AVM FritzBox 7270发送Tr064请求,c#,soap,upnp,fritzbox,C#,Soap,Upnp,Fritzbox,我正在学习如何使用C向我的AVM FritzBox 7270路由器发送SOAP请求 下面是我向路由器发送SOAP请求的方法: private string Execute(string controlUrl, string serviceType, string action) { WebRequest webRequest = WebRequest.Create("http://fritz.box:49000" + controlUrl); Ht

我正在学习如何使用C向我的AVM FritzBox 7270路由器发送SOAP请求

下面是我向路由器发送SOAP请求的方法:

    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;
        }
    }
函数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);
        }
    }
这很有效

然而,我认为我应该走正式的道路,首先发出一个请求

http://fritz.box:49000/tr64desc.xml
接收对我的实际路由器有效的参数。在对该请求的响应中,我发现以下节点:

<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>
使用serviceId和controlUrl的这些值,我得到错误500内部服务器错误


谁能帮我?我的代码出了什么问题?

我想问题已经解决了:

Fritz.Box似乎有很多未记录的特性,而我在互联网上找到的pararmeters是影院特性的一部分


没有问题。

这可能是设备中的错误。。。但是,您如何知道应该使用的服务描述?你真的是从SSDP发现中得到的吗?
<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>
<service>
  <serviceType>urn:dslforum-org:service:WANPPPConnection:1</serviceType>
  <serviceId>urn:WANPPPConnection-com:serviceId:WANPPPConnection1</serviceId>
  <controlURL>/upnp/control/wanpppconn1</controlURL>
  <eventSubURL>/upnp/control/wanpppconn1</eventSubURL>
  <SCPDURL>/wanpppconnSCPD.xml</SCPDURL>
</service>
GetExternalIPAddress