c# - 向 FritzBox 发送 SOAP 请求

标签 c# .net soap fritzbox

我正在尝试使用 C# 向 FritzBox(WLAN 路由器)发送 SOAP 请求。 目前我正在手动组装 XML SOAP 请求,因为服务器不提供 WSDL 文件。

这就是我创建附加 XML 的 Web 请求的方式:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create (@"http://fritz.box:49000/igdupnp/control/WANIPConn1");
webRequest.Headers.Add (@"SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";

但是在发送请求时我得到了一个 WebException:

The remote server returned an error: (404) Not Found.

我很确定它必须对我的 URL 执行某些操作,但我不知道如何修复它。 FritzBox 文档相当糟糕:-/

最佳答案

尝试使用此代码获取外部 IP

WebRequest webRequest = WebRequest.Create("http://fritz.box:49000/igdupnp/control/WANIPConn1");
        HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
        httpRequest.Method = "POST";
        httpRequest.ContentType = "text/xml; charset=utf-8";
        httpRequest.Headers.Add("SOAPACTION", "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress");
        httpRequest.ProtocolVersion = HttpVersion.Version11;
        httpRequest.Credentials = CredentialCache.DefaultCredentials;
        Stream requestStream = httpRequest.GetRequestStream();
        //Create Stream and Complete Request             
        StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);

        StringBuilder soapRequest = new StringBuilder(@"<?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:GetExternalIPAddress xmlns:u=""urn:schemas-upnp-org:service:WANIPConnection:1"" /></s:Body></s:Envelope>");

        streamWriter.Write(soapRequest.ToString());
        streamWriter.Close();
        //Get the Response    
        HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
        StreamReader srd = new StreamReader(wr.GetResponseStream());
        string resulXmlFromWebService = srd.ReadToEnd();
        return resulXmlFromWebService;

关于c# - 向 FritzBox 发送 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38926729/

相关文章:

soap - 如何在 savon 中将 xmlns 添加到消息中

php - 为新的 Api 使用 SOAP 或 REST

java - 将 json 对象添加到 Java HttpGet 请求到 C# WebApi

java - Android SOAP wsdl

c# - 使用鼠标滚轮 XNA 缩放图像

c# - 关闭后如何使 Window.Current 可见?

c# - JSON.NET 部分更新 Rest API 客户端

c# - Silverlight 验证。电子邮件验证问题

c# - Base64反序列化过程中的空引用异常(C#)

c# - 将列嵌套在另一列中