我尝试使用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:
远程服务器返回错误:(404)Not Found。
我很确定它必须对我的网址做些什么,但我不知道如何解决它。 FritzBox文档相当糟糕: - /
答案 0 :(得分:0)
尝试使用此代码来获取外部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;
答案 1 :(得分:0)
这是另一个常见用例的示例:获取新的IP地址
private string ReconnectFritzBox()
{
string xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope\">" +
"<s:Body>" +
"<u:ForceTermination xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\" />" +
"</s:Body>" +
"</s:Envelope>";
string resulXmlFromWebService = null;
var webRequest = WebRequest.Create("http://fritz.box:49000/igdupnp/control/WANIPConn1");
var httpRequest = (HttpWebRequest)webRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml; charset=utf-8";
httpRequest.Headers.Add("SOAPACTION", "urn:schemas-upnp-org:service:WANIPConnection:1#ForceTermination");
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.Credentials = CredentialCache.DefaultCredentials;
httpRequest.ContentLength = xmldata.Length;
using (var requestStream = httpRequest.GetRequestStream())
{
//Create Stream and Complete Request
using (var streamWriter = new StreamWriter(requestStream, Encoding.ASCII))
{
streamWriter.Write(xmldata);
streamWriter.Close();
//Get the Response
var wr = (HttpWebResponse)httpRequest.GetResponse();
using (var srd = new StreamReader(wr.GetResponseStream()))
{
resulXmlFromWebService = srd.ReadToEnd();
}
}
}
return resulXmlFromWebService;
}
完整代码示例也在Github上:Link