Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell invoke-webrequest with xml soap envelope body containing £ char errors

I'm trying to use Powershells Invoke-Webrequest to send a soap envelope to a password protected web service. The password contains the '£' char, which is causing the following error:

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>

This is the script (sensitive information removed):

[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

It doesnt seem to matter what type/encoding I force the $SOAP to be, Invoke-Webrequest insists on interpreting the '£' as a '�'.

Any ideas?

like image 488
ItsNotMyStrongPoint Avatar asked Sep 29 '22 08:09

ItsNotMyStrongPoint


1 Answers

It certainly looks like an encoding issue, so here's a few things you can try.

Make sure your file is saved as UTF-8. Use notepad.exe to open it and select Save As. At the bottom there's an "Encoding" dropdown. It should say UTF-8. If not, change it and overwrite the file.

In Powershell print out the file using the type or get-content command. The pound sign should be displayed as a pound sign, not like a question mark. Saving it using notepad (or another editor) in the correct encoding should fix this.

Another thing that might work is storing the SOAP message in a separate file encoded as UTF-8 (or Unicode). Then get the content of the file using Get-Content using the -Encoding parameter and the type you saved it with. Again, this is to make sure the pound sign isn't mangled before it's used in the service call.

If that doesn't work, maybe you can get the password using Read-Host and combine the SOAP message using that.

Last, you can always change the password so it has no characters in the unicode range. You can still use a bunch of special characters as long as they are in the lower ASCII range. E.g. %=+-)ç!"# etc. But that's a last resort I guess.

like image 148
jurgenb Avatar answered Oct 02 '22 09:10

jurgenb