Powershell invoke-webrequest with xml soap envelope body containing £ char 错误
Powershell invoke-webrequest with xml soap envelope body containing £ char errors
我正在尝试使用 Powershells Invoke-Webrequest 将 soap 信封发送到受密码保护的 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 坚持将“£”解释为“�”。
有什么想法吗?
这看起来确实像是编码问题,因此您可以尝试以下几种方法。
确保您的文件保存为 UTF-8。使用 notepad.exe 打开它并 select 另存为。在底部有一个 "Encoding" 下拉菜单。它应该说UTF-8。如果不是,请更改并覆盖文件。
在 Powershell 中使用 type
或 get-content
命令打印文件。井号应该显示为井号,而不是问号。
使用记事本(或其他编辑器)以正确的编码保存它应该可以解决这个问题。
另一件可行的事情是将 SOAP 消息存储在编码为 UTF-8(或 Unicode)的单独文件中。
然后使用 Get-Content
使用 -Encoding 参数和保存文件的类型获取文件的内容。
同样,这是为了确保井号在用于服务调用之前不会被损坏。
如果这不起作用,也许您可以使用 Read-Host
获取密码并使用它合并 SOAP 消息。
最后,您可以随时更改密码,使其不包含 unicode 范围内的字符。您仍然可以使用一堆特殊字符,只要它们在较低的 ASCII 范围内即可。例如。 %=+-)ç!"# 等等。
但我想这是最后的手段。
我正在尝试使用 Powershells Invoke-Webrequest 将 soap 信封发送到受密码保护的 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 坚持将“£”解释为“�”。
有什么想法吗?
这看起来确实像是编码问题,因此您可以尝试以下几种方法。
确保您的文件保存为 UTF-8。使用 notepad.exe 打开它并 select 另存为。在底部有一个 "Encoding" 下拉菜单。它应该说UTF-8。如果不是,请更改并覆盖文件。
在 Powershell 中使用 type
或 get-content
命令打印文件。井号应该显示为井号,而不是问号。
使用记事本(或其他编辑器)以正确的编码保存它应该可以解决这个问题。
另一件可行的事情是将 SOAP 消息存储在编码为 UTF-8(或 Unicode)的单独文件中。
然后使用 Get-Content
使用 -Encoding 参数和保存文件的类型获取文件的内容。
同样,这是为了确保井号在用于服务调用之前不会被损坏。
如果这不起作用,也许您可以使用 Read-Host
获取密码并使用它合并 SOAP 消息。
最后,您可以随时更改密码,使其不包含 unicode 范围内的字符。您仍然可以使用一堆特殊字符,只要它们在较低的 ASCII 范围内即可。例如。 %=+-)ç!"# 等等。 但我想这是最后的手段。