【问题标题】:Powershell invoke-webrequest with xml soap envelope body containing £ char errors带有包含 £ char 错误的 xml soap 信封主体的 Powershell 调用-webrequest
【发布时间】:2015-06-13 08:14:43
【问题描述】:

我正在尝试使用 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 坚持将“£”解释为“�”。

有什么想法吗?

【问题讨论】:

  • 您的脚本文件本身是 unicode 还是 utf-8 编码的?
  • 它是由代理动态生成的(不是我写的)-但是如果我将输出粘贴到 ISE 中,我会遇到同样的问题,所以无论 Windows 2012 R2 上的 ISE 编码如何(PS 4.0) 默认我猜。

标签: xml powershell soap encoding


【解决方案1】:

这看起来肯定是编码问题,所以您可以尝试以下一些方法。

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

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

另一个可行的方法是将 SOAP 消息存储在编码为 UTF-8(或 Unicode)的单独文件中。 然后使用 Get-Content 使用 -Encoding 参数和保存它的类型来获取文件的内容。 同样,这是为了确保井号在用于服务调用之前不会被损坏。

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

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

【讨论】:

  • 这里也有一些可行的方法,将井号编码为字符串中的 unicode:stackoverflow.com/questions/1056692/…
  • 我意识到这是很久以前的事了,但问题显示为未回答,所以将回答它,不令人满意。由于脚本是通过将替换 {{variables}} 替换为文本的过程动态生成的,因此无法找出编码是什么。我最终作弊并更改了密码,因此没有£符号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多