This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17246|Reply: 0

[WinForm] WinForm develops the browser, and WebBrowser obtains the page content and solves Chinese garbled characters

[Copy link]
Posted on 10/11/2015 10:14:11 PM | | |
C# writes a simple browser and encounters a small problem, namely: the page content obtained through WebBrowser.DocumentText is garbled

System.IO.StreamReader getReader = new System.IO.StreamReader(this.webBrowser.DocumentStream, System.Text.Encoding.GetEncoding("gb2312"));
string gethtml = getReader.ReadToEnd();

Note: The page content obtained from the DocumentText property is not used here.

UTF8 encoding method, just specify UTF8 when reading.
System.IO.StreamReader getReader = new System.IO.StreamReader(this.webBrowser.DocumentStream, System.Text.Encoding.GetEncoding("utf8"));


The encoding of the WebBrowser can be obtained from the document object, just change the code to the following.

System.IO.StreamReader getReader = new System.IO.StreamReader(this.webBrowser.DocumentStream, System.Text.Encoding.GetEncoding(this.webBrowser.Document.Encoding));
string gethtml = getReader.ReadToEnd();





Previous:Zhou Jinqiao: IT people (programmers), please take care of your body
Next:The current thread is not in a single-threaded unit, so the ActiveX control "8856f961-340a-11d0-a96" cannot be instantiated
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com