我正在尝试将GetProtocolInfo SOAP请求发送到Upnp MediaRenderer(在本例中为Windows Media Player)。 我跟着tutorial,但每次发送请求时,服务器返回400。
这就是Fiddler给我的要求:
POST http://192.168.2.101:2869/upnphost/udhisapi.dll?control=uuid:1dc2bf33-6efe-41dd-8139-a98b9f5ca2e0+urn:upnp-org:serviceId:ConnectionManager HTTP/1.1
Accept: */ *
Content-Length: 306
Accept-Encoding: identity
Connection: Close
Host: 192.168.2.101:2869
SOAPAction: "urn:schemas-upnp-org:service:ConnectionManager:1#GetProtocolInfo"
Content-Type: text/xml; charset=utf-8
User-Agent: NativeHost
Cache-Control: no-cache
<?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:GetProtocolInfo xmlns:u="urn:schemas-upnp-org:service:ConnectionManager:1">
</u:GetProtocolInfo>
</s:Body>
</s:Envelope>
我生成SOAP请求的函数:
private static HttpRequestMessage MakeSoapRequest(Uri requestedUri, XElement SoapAction, String[] args)
{
try
{
HttpRequestMessage m = new HttpRequestMessage(HttpMethod.Post, requestedUri);
string attributes = "";
if (args.Length > 0)
{
for (int i = 0; i <= args.Length; i = i + 2)
{
attributes += "<" + args[i] + ">" + args[i + 1] + "</" + args[i] + ">";
}
}
string strDoc = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "\r\n"+
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + "\r\n" +
"<s:Body>" + "\r\n" +
"<" + SoapAction.Name.LocalName + " " + "xmlns=\"" + SoapAction.Name.NamespaceName + "\">" + "\r\n" +
attributes + "\r\n" +
"</" + SoapAction.Name.LocalName + ">" + "\r\n" +
"</s:Body>" + "\r\n" +
"</s:Envelope>";
m.Headers.Host = requestedUri.Authority;
m.Content = new StringContent("\r\n" + strDoc, Encoding.UTF8, "text/xml");
m.Content.Headers.ContentLength = strDoc.Length;
m.Headers.Add("SOAPAction", "\"" + SoapAction.Name.NamespaceName + "#" + SoapAction.Name.LocalName + "\"");
return m;
}
catch (Exception e)
{
App.exceptionHandler(e);
return null;
}
}
实际发送请求的函数:
private static async Task<XDocument> GetXmlAsync(HttpClient http, HttpRequestMessage request)
{
try
{
HttpResponseMessage response = await http.SendAsync(request);
response.EnsureSuccessStatusCode();
if (!(response.Content.Headers.ContentType.CharSet == null))
{
response.Content.Headers.ContentType.CharSet = response.Content.Headers.ContentType.CharSet.Trim('"');
}
string body = await response.Content.ReadAsStringAsync();
return XDocument.Load(new StringReader(body));
}
catch (Exception e)
{
App.exceptionHandler(e);
return null;
}
}
感谢您的帮助:)
答案 0 :(得分:1)
您是否输出了实际的消息内容?
您的\r\n
后可能需要额外</s:Envelope>
。
以下是标题和内容应该是什么样子的示例:
POST /ConnectionManager/ctrl HTTP/1.1
Host: 192.168.0.122:8080
Content-Length: 299
Content-Type: text/xml; charset="utf-8"
SOAPAction: "urn:schemas-upnp-org:service:ConnectionManager:1#GetProtocolInfo"
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetProtocolInfo xmlns:u="urn:schemas-upnp-org:service:ConnectionManager:1">
</u:GetProtocolInfo>
</s:Body>
</s:Envelope>