【问题标题】:Upnp: GetProtocolInfo returns 400 (Bad request)Upnp:GetProtocolInfo 返回 400(错误请求)
【发布时间】:2015-03-22 15:17:59
【问题描述】:

我正在尝试将 GetProtocollInfo 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;
    }
}

感谢您的帮助:)

【问题讨论】:

    标签: c# soap windows-phone-8.1 upnp


    【解决方案1】:

    您有实际消息内容的输出吗?
    &lt;/s:Envelope&gt; 之后,您可能需要一个额外的\r\n

    以下是标题和内容的示例:

    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>
    

    【讨论】:

    • 最后的 blanc 行没有帮助:/
    • 您可能想在 标记后尝试 2 个空行。 信封>\r\n\r\n
    • 我解决了。这个问题实际上是由一开始的 blanc 行引起的。我会把你的答案作为解决方案:)谢谢:)
    猜你喜欢
    • 2019-07-28
    • 2019-07-11
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多