0

如何在 Lua 中向我的 Sonos 扬声器发送一个简单的 HTTP POST/GET SOAP 请求?

我已经成功地尝试了简单的 HTTP POST 和 GET 请求,但我不知道从哪里开始使用 SOAP 请求。

注意:我是这方面的新手。我以前从未使用过 NodeMCU,也没有在 Lua 中编程过。不过我有其他语言的经验。

我知道如何在 C#、Java 和 PHP 中做到这一点。

这适用于邮递员:

HTTP 标头:

SOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause
Content-Type:text/xml; charset="utf-8"
Host:192.168.0.10:1400

身体:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Pause xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>

我所做的是这样,但它不起作用:

        sendRequest("192.168.0.10")
function sendRequest(url)
    print("Sending request to Sonos Playbar...")
    sk = net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c) print(c) end )
    sk:on("connection", function(sck, c)

        print("\r\n\r\n\r\n")

        -- HTTP 405: Method not allowed
        -- sck:send("POST / HTTP/1.1\r\nHost: "..url..":1400\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

        -- HTTP 500, UPnP 402: Invalid arguments
        -- sck:send("POST /MediaRenderer/AVTransport/Control HTTP/1.1\r\nHost: "..url..":1400\r\nSOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause\r\nConnection: keep-alive\r\n\r\nAccept: */*\r\n\r\n")


        local content = nil;
    content = "POST /MediaRenderer/AVTransport/Control\r\n"
    content = content.."Host:192.168.0.10:1400\r\n"
    content = content.."Content-Type:text/xml; charset=utf-8\r\n"
    content = content.."SOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause\r\n"
    content = content.."\r\n"

    -- SOAP Body
    content = content.."<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""
    content = content.." s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
    content = content.."<s:Body>"
    content = content.."<u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"
    content = content.."<InstanceID>0</InstanceID>"
    content = content.."</u:Pause>"
    content = content.."</s:Body>"
    content = content.."</s:Envelope>"
    -- SOAP Body End

        print(content.."\r\n\r\n\r\n")

        sck:send(content);
    end)
    sk:connect(1400, url)
end

我收到了我的 Sonos 播放器的回复:

HTTP/1.1 500 Internal Server Error
CONTENT-LENGTH: 347
CONTENT-TYPE: text/xml; charset="utf-8"
EXT: 
Server: Linux UPnP/1.0 Sonos/34.16-37101 (ZPS9)
Connection: close

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <s:Body>
        <s:Fault>
            <faultcode>s:Client</faultcode>
                <faultstring>UPnPError</faultstring>
                    <detail>
                        <UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
                            <errorCode>401</errorCode>
                        </UPnPError>
                    </detail>
        </s:Fault>
    </s:Body>
</s:Envelope>

我究竟做错了什么?基本上,我复制并粘贴了文本。也许是标题的顺序?也许我在声明标题错误或什么?

4

2 回答 2

2

我没有可玩的 Sonos 设备。因此,这不是一个确定的答案。

变量中的字符串content不是有效的 HTTP 请求。Sonos 不理解它,因为错误代码 401 的意思是“无效操作”

您需要单独的 HTTP 标头和\r\n. \r\n需要在 HTTP 正文之前放置一个额外的内容。因此,我希望您content应该是:

"POST http://192.168.0.10:1400/MediaRenderer/AVTransport/Control\r\n
SOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause\r\n
Content-Type:text/xml; charset=\"utf-8\"\r\n
Host:192.168.0.10:1400\r\n\r\n
<s:Envelope xmlns:s=\"http://schemas.xml......"
于 2017-01-28T21:35:55.073 回答
0

最后!我有它的工作!下面是让它工作的代码:

sendRequest("192.168.0.10")
function sendRequest(url)
    print("Sending request to Sonos Playbar...")
    local content = nil;
    content = "";
    -- SOAP Body
    content = content.."<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""
    content = content.." s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
    content = content.."<s:Body>"
    content = content.."<u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"
    content = content.."<InstanceID>0</InstanceID>"
    content = content.."</u:Pause>"
    content = content.."</s:Body>"
    content = content.."</s:Envelope>"
    -- SOAP Body End

    http.post("http://"..url..":1400/MediaRenderer/AVTransport/Control",
        'Content-Type: text/xml; charset="utf-8"\r\n'..
        'Host:'..url..':1400\r\n'..
        'SOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause\r\n',
        content, function(code, data)
            if(code < 0) then
                print("HTTP request failed with code "..code)
            else
                print(code, data)
            end
        end)
end
于 2017-01-31T12:09:06.393 回答