Please feel free to provide feedback or file bugs here.

Invoke-RestMethod and Invoke-WebRequest Encoding bug

Invoke-RestMethod and Invoke-WebRequest Encoding bug

This is 4 years old bug,from Invoke-RestMethod and Invoke-WebRequest was born.

【Cause of the bug】
Utf8 web page incorrectly identified as iso8859-1.
Mistakenly convert THE! utf8("false ISO-8859-1") source string to utf8.
There may be more encoding incorrectly identified,encoding incorrectly conversion errors.

【bug with powershell version】
all version
powershell 5.0 with windows10 10586 :yes tested.

【test code】
Invoke-WebRequest -Uri 'http://www.msn.com'; # return chinese messy code
(Invoke-WebRequest -Uri 'http://www.msn.com').BaseResponse.CharacterSet # utf8 web page,but return ISO-8859-1

Invoke-RestMethod -Uri 'http://www.msn.com';

【how to fix】
convert string encoding.
from utf8 to THE! "false ISO-8859-1"

$utf8 = [System.Text.Encoding]::GetEncoding(65001)
$iso88591 = [System.Text.Encoding]::GetEncoding(28591) #ISO 8859-1 ,Latin-1

$wrong_string = Invoke-RestMethod -Uri 'http://www.msn.com';
$wrong_bytes = $utf8.GetBytes($wrong_string)

$right_bytes = [System.Text.Encoding]::Convert($utf8,$iso88591,$wrong_bytes) #Look carefully
$right_string = $utf8.GetString($right_bytes) #Look carefully
write-host $right_string

【reporter】
I've been searching this bugs history list, I was the first reporter.
By the chinese powershell study & research group. QQ group No. is 183173532

【date】
2016-05-01

20 votes
Sign in
(thinking…)
Sign in with: Facebook Google
Signed in as (Sign out)

We’ll send you updates on this idea

响叮咚 shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

3 comments

Sign in
(thinking…)
Sign in with: Facebook Google
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base