5

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?

ItsNotMyStrongPoint
  • 137
  • 1
  • 4
  • 10
  • Is your script file itself unicode or utf-8 encoded? – jurgenb Apr 08 '15 at 12:37
  • Its being generated on the fly by an agent (not written by me) - but if I take the output and paste into ISE I have the same problem, so whatever encoding ISE on Windows 2012 R2 (PS 4.0) defaults to I guess. – ItsNotMyStrongPoint Apr 08 '15 at 14:42

1 Answers1

4

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.

jurgenb
  • 472
  • 4
  • 11
  • here's something that might also work, encode that pound sign as unicode in the string: http://stackoverflow.com/questions/1056692/how-to-encode-unicode-character-codes-in-a-powershell-string-literal – jurgenb Apr 08 '15 at 19:46
  • I realise this was a long time ago but the question is showing as unanswered, so will answer it, unsatisfactorily. As the script was being generated on the fly by a process that replaced substitution {{variables}} with text, it was impossible to find out what the encoding was. I ended up cheating and getting the password changed so it didn't have the £ sign in it. – ItsNotMyStrongPoint Apr 28 '16 at 15:41