Home > Net >  C # about HttpWebResponse returned Chinese garbled
C # about HttpWebResponse returned Chinese garbled

Time:09-19

Accept HttpWebResponse has been gibberish, still didn't understand, ask the great spirit show
Online demo code is
 HttpWebResponse response=(HttpWebResponse) request. The method GetResponse (); 
StreamReader reader=new StreamReader (response. GetResponseStream (), Encoding. The Default).
The string result=reader. ReadToEnd ();


But I output the result in Chinese are garbled,

Then I have to add transcoding can is right
 HttpWebResponse response=(HttpWebResponse) request. The method GetResponse (); 
StreamReader reader=new StreamReader (response. GetResponseStream (), Encoding, UTF8);
Byte [] temp;
Temp=Encoding. UTF8. GetBytes (reader. ReadToEnd ());
Temp=Encoding. The Convert (Encoding. GetEncoding (" utf-8 "), Encoding, GetEncoding (" GB2312 "), temp);
The string result=Encoding. The Default. Get string (temp);


How can I optimize??

CodePudding user response:

Should be the cause of the website, the website has different coding,

CodePudding user response:

To understand each other's coding, their request in the code to decode, generally is UTF8, but does not appoint encoding, decoding is GB2312 by default
  •  Tags:  
  • C#
  • Related