Xml Powershell使用包含£;字符错误
我正在尝试使用Powershells Invoke Webrequest向受密码保护的web服务发送soap信封。密码包含“£”字符,导致以下错误:Xml Powershell使用包含£;字符错误,xml,powershell,soap,encoding,Xml,Powershell,Soap,Encoding,我正在尝试使用Powershells Invoke Webrequest向受密码保护的web服务发送soap信封。密码包含“£”字符,导致以下错误: 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/:pas
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>
Invoke WebRequest…格式化程序在尝试反序列化消息时引发异常:尝试反序列化参数时出错http://tempuri.org/:password. InnerException消息为“反序列化System.String类型的对象时出错。”�中间的“包含无效的UTF8字节”。有关更多详细信息,请参阅InnerException。
这是脚本(已删除敏感信息):
[xml]$SOAP='0
密码文本在中间
'
$headers=@{“SOAPAction”=“http://tempuri.org/service/MethodName"}
$URI=”https:///service.svc"
$out=Invoke WebRequest$uri-Method post-ContentType'text/xml'-Body$SOAP-Headers$Headers
我强迫$SOAP是什么类型/编码似乎并不重要,Invoke Webrequest坚持将“£”解释为�'.
有什么想法吗?这看起来确实像是一个编码问题,所以这里有一些东西你可以试试 确保您的文件保存为UTF-8。使用notepad.exe打开它并选择另存为。在底部有一个“编码”下拉列表。应该是UTF-8。如果没有,请更改它并覆盖该文件 在Powershell中,使用
键入
或获取内容
命令打印文件。英镑符号应显示为英镑符号,而不是问号。
使用记事本(或其他编辑器)以正确的编码保存应该可以解决这个问题
另一个可行的方法是将SOAP消息存储在一个单独的文件中,该文件编码为UTF-8(或Unicode)。
然后使用-Encoding参数和保存文件的类型,使用get content
获取文件内容。
同样,这是为了确保英镑符号在服务调用中使用之前不会被弄坏
如果这不起作用,也许您可以使用readhost
获取密码,并使用该密码组合SOAP消息
最后,您可以随时更改密码,使其在unicode范围内没有字符。您仍然可以使用一组特殊字符,只要它们在较低的ASCII范围内。例如%=+-)ç!“等等。
但我想这是最后的办法。脚本文件本身是unicode还是utf-8编码的?它是由代理(不是我写的)动态生成的-但如果我将输出粘贴到ISE中,我也会遇到同样的问题,因此无论在Windows 2012 R2(PS 4.0)上使用何种编码ISE默认值为我猜。这里有一些可能也会起作用,在字符串中将磅符号编码为unicode:我意识到这是很久以前的事了,但问题显示为未回答,所以会回答它,但不令人满意。因为脚本是由替换{{变量}的进程动态生成的有了文本,就不可能知道编码是什么。我最终作弊并更改了密码,所以里面没有签名。
[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