简体   繁体   中英

Upnp: GetProtocolInfo returns 400 (Bad request)

I am trying to send a GetProtocollInfo SOAP request to an Upnp MediaRenderer (In this case Windows Media Player). I followed that tutorial , but everytime I send my request, the server returns 400.

This is what Fiddler gives me for the request:

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>

My function that generates the SOAP request:

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;
    }
}

The function that actually sends the request:

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;
    }
}

Thanks for your help :)

Do you have output of your actual message content?
You may need an extra \\r\\n after your </s:Envelope> .

Here is an example of what it should all look like with header and content:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM