Problem: In using WebRequest to get the source code is garbled.
Reason: 1, wrong encoding
WORKAROUND: Set the corresponding encoding
WebRequest request = WebRequest.Create (URL);
WebResponse response = await request. Getresponseasync ();
Stream stream = Response. GetResponseStream ();
StreamReader reader = new StreamReader (stream, encoding.getencoding (coding));//The coding here is the encoding of the page, you can use IE right-click to view the code.
Result = reader. ReadToEnd ();
Reader. Dispose ();
Reader. Dispose ();
2, the page is compressed
See if the HTML head,contentencoding is gzip-if yes, you need to unzip it. The following code is under WINRT.
WebRequest request = WebRequest.Create (URL);
WebResponse response = await request. Getresponseasync ();
Debug.WriteLine (((httpwebresponse) response). Statusdescription);
if (response. Headers.AllKeys.Contains ("content-encoding") && response. headers["Content-encoding"]. ToLower () = = "gzip")//Unzip first if using gzip
{
using (System.IO.Stream streamreceive = response. GetResponseStream ())
{
using (var ZipStream =
New System.IO.Compression.GZipStream (Streamreceive, System.IO.Compression.CompressionMode.Decompress))
{
using (StreamReader sr = new System.IO.StreamReader (ZipStream, encoding.getencoding (coding)))
{
Result = Sr. ReadToEnd ();
}
}
}
}