Powershell使用包含£char错误的xml soap信封主体调用webrequest

时间:2015-04-08 11:37:08

标签: xml powershell soap encoding

我正在尝试使用Powershells Invoke-Webrequest将肥皂信封发送到受密码保护的Web服务。密码包含'£'字符,这会导致以下错误:

Invoke-WebRequest ...The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:password. The InnerException message was 'There was an error deserializing the object of type System.String. '�inthemiddle' contains invalid UTF8 bytes.'.  Please see InnerException for more details.</faultstring></s:Fault></s:Body></s:Envelope>

这是脚本(已删除敏感信息):

[xml]$SOAP = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:MethodName>
         <tem:password>passwordtextwit£inthemiddle</tem:password>
      </tem:MethodName>
   </soapenv:Body>
</soapenv:Envelope>'

$headers = @{"SOAPAction" = "http://tempuri.org/service/MethodName"}

$URI = "https://<MY URI.com>/service.svc"
$out = Invoke-WebRequest $uri -Method post -ContentType 'text/xml' -Body $SOAP -Headers $headers

我强迫$ SOAP使用什么类型/编码似乎无关紧要,Invoke-Webrequest坚持将'£'解释为' '。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

它当然看起来像编码问题,所以这里有一些你可以尝试的东西。

确保您的文件保存为UTF-8。使用notepad.exe打开它,然后选择另存为。在底部有一个“编码”下拉列表。应该说是UTF-8。如果没有,请更改它并覆盖该文件。

在Powershell中,使用typeget-content命令打印文件。英镑符号应显示为英镑符号,而不是问号。 使用记事本(或其他编辑器)以正确的编码保存它应该解决这个问题。

另一件可能有用的事情是将SOAP消息存储在一个编码为UTF-8(或Unicode)的单独文件中。 然后使用-Encoding参数和您保存的类型使用Get-Content获取文件的内容。 同样,这是为了确保英镑符号在服务调用中使用之前不会被破坏。

如果这不起作用,也许你可以使用Read-Host获取密码并使用它来组合SOAP消息。

最后,您始终可以更改密码,使其在unicode范围内没有字符。只要它们处于较低的ASCII范围内,您仍然可以使用一堆特殊字符。例如。 %= + - )ç!“#等 但这是我猜的最后一招。

相关问题