【问题标题】:Bad request from Soap call to UPNP TV从 Soap 呼叫到 UPNP 电视的错误请求
【发布时间】: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 对追踪我的请求和格式正确的请求之间的差异有很大帮助。

标签: c# soap upnp dlna


【解决方案1】:

UPnP architecture spec 声明动作调用的格式是

POST path of control URL HTTP/1.1
HOST: host of control URL:port of control URL
CONTENT-LENGTH: bytes in body
CONTENT-TYPE: text/xml; charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:service:serviceType:v#actionName"

<?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:actionName xmlns:u="urn:schemas-upnp-org:service:serviceType:v">
      <argumentName>in arg value</argumentName>
       other in args and their values go here, if any
    </u:actionName>
  </s:Body>
</s:Envelope>

您的代码在几个方面偏离了这一点:

  • HTTP 标头之间没有换行符 (\r\n)(除非这是您问题中的格式错误)
  • POST 标头中的路径不应包含方案、主机或端口。即删除http://192.168.1.4:52323

您可能还想考虑删除Expect 标头的使用。添加它似乎没有任何好处(您不应该期望在本地网络上遇到任何支持此标头但无法处理几百字节请求正文的开销的服务器/代理)。相反,您完全有可能遇到不知道如何处理此标头的原始 HTTP 服务器,因此拒绝请求。

【讨论】:

  • 感谢您的信息!我认为标题之间有新行,这只是从提琴手复制它的格式问题。我编辑了原始帖子以展示我如何在 C# 中构建 webRequest。我可以尝试删除期望标头并修复我的 POST 值。
猜你喜欢
  • 1970-01-01
  • 2018-02-23
  • 2012-06-03
  • 1970-01-01
  • 2022-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多