Upnp:GetProtocolInfo returns 400(错误请求)

Upnp: GetProtocolInfo returns 400 (Bad request)

我正在尝试将 GetProtocollInfo SOAP 请求发送到 Upnp MediaRenderer(在本例中为 Windows 媒体播放器)。 我跟着 tutorial,但每次我发送请求时,服务器 returns 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;
    }
}

感谢您的帮助:)

你有实际消息内容的输出吗?
</s:Envelope>.

之后,您可能需要一个额外的 \r\n

这里是 header 和内容应该是什么样子的示例:

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>