【发布时间】:2017-03-12 23:48:00
【问题描述】:
我一直在尝试向启用了 Upnp 的电视发送命令。我编写了一些 C# 代码来请求网络上的 UPNP 设备,然后通过肥皂向电视发送命令。但是,当我向电视提出请求时,我不断收到 400 个错误请求。我在 Fiddler 中观看过,这似乎是 RAW 请求。
POST http://192.168.1.4:52323/upnp/control/RenderingControl HTTP/1.1 SOAPACTION: urn:schemas-upnp-org:service:RenderingControl:1#SetVolume Content-Type: text/xml;charset="utf-8" Accept: text/xml Host:
192.168.1.4:52323 Content-Length: 418 Expect: 100-continue Connection: Keep-Alive
<?xml version="1.0" encoding="utf-8"?> <s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body>
<ns0:SetVolume xmlns:ns0="urn:schemas-upnp-org:service:RenderingControl:1">
<InstanceId>0</InstanceId>
<Channel>Master</Channel>
<DesiredVolume>70</DesiredVolume>
</ns0:SetVolume> </s:Body> </s:Envelope>
有人觉得这个请求有什么不好的地方吗?
这是我在 C# 中构建 WebRequest 的方式:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPACTION", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
更新
我试图完全删除代码,并通过提琴手形成请求。我目前没有将电视连接到我的网络,所以我正在尝试测试对我的路由器的呼叫。这是我通过提琴手新建的调用:
POST http://10.0.0.1:49153/upnp/control/Layer3Forwarding HTTP/1.1
SOAPACTION: urn:schemas-upnp-org:service:Layer3Forwarding:1#GetDefaultConnectionService
Content-Type: text/xml;charset="utf-8"
Host: 10.0.0.1:49153
Content-Length: 345
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ns0:GetDefaultConnectionService xmlns:ns0="urn:schemas-upnp-org:service:Layer3Forwarding:1">
</ns0:GetDefaultConnectionService>
</s:Body>
</s:Envelope>
但是我不断收到无效的操作响应或错误的 xml,例如下面的响应:
HTTP/1.1 500 Internal Server Error
CONTENT-LENGTH: 413
CONTENT-TYPE: text/xml; charset="utf-8"
DATE: Tue, 14 Mar 2017 21:55:49 GMT
EXT:
SERVER: Linux/3.12.14, UPnP/1.0, Portable SDK for UPnP devices/1.6.18
X-User-Agent: redsonic
<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>-111</errorCode>
<errorDescription>Invalid Action</errorDescription>
</UPnPError>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
关于我的请求可能会如何失败的任何想法?
【问题讨论】:
-
在 POST 行中将
control/Layer3Forwarding交换为Layer3Forwarding/control怎么样?这就是它在我的设备上的样子。 -
感谢您的建议,我认为我的问题是我的 SOAPACTION 不在引号中。我现在似乎在提琴手中得到回应。 Device Spy 对追踪我的请求和格式正确的请求之间的差异有很大帮助。