Its*_*int 5 xml powershell encoding soap
我正在尝试使用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>
Run Code Online (Sandbox Code Playgroud)
这是脚本(敏感信息已删除):
[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
Run Code Online (Sandbox Code Playgroud)
我强制$ SOAP使用哪种类型/编码似乎并不重要,Invoke-Webrequest坚持将“£”解释为“?”。
有任何想法吗?
它看起来确实像是编码问题,因此您可以尝试以下一些操作。
\n\n确保您的文件保存为 UTF-8。使用 notepad.exe 打开它并选择“另存为”。底部有一个“编码”下拉列表。应该是UTF-8。如果不是,请更改它并覆盖该文件。
\n\ntype在 Powershell 中使用或命令打印出文件get-content。井号应显示为井号,而不是像问号。\n使用记事本(或其他编辑器)以正确的编码保存它应该可以解决此问题。
另一种可能有效的方法是将 SOAP 消息存储在编码为 UTF-8(或 Unicode)的单独文件中。\n然后使用Get-Content-Encoding 参数和保存该文件的类型来获取该文件的内容。\n同样,此操作是为了确保井号在服务调用中使用之前不会被破坏。
如果这不起作用,也许您可以使用获取密码Read-Host并使用它组合 SOAP 消息。
最后,您可以随时更改密码,使其不包含 unicode 范围内的字符。您仍然可以使用一堆特殊字符,只要它们位于较低的 ASCII 范围内即可。例如 %=+-)\xc3\xa7!"# 等\n但我想这是最后的手段。
\n