This method needs to pass the Encoding method of the target webpage, such as System. Text. Encoding. Default or System. Text. Encoding. UTF8.
If any expert knows how to automatically determine the encoding format of the target page, please let us know in the comments. Thank you!
///
/// Obtain the source code
///
///
///
///
Public static string GetPage (string url, Encoding encoding)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader reader = null;
Try
{
Request = (HttpWebRequest) WebRequest. Create (url );
Request. UserAgent = "www.svnhost.cn ";
Request. Timeout = 20000;
Request. AllowAutoRedirect = false;
Response = (HttpWebResponse) request. GetResponse ();
If (response. StatusCode = HttpStatusCode. OK & response. ContentLength <1024*1024)
{
Reader = new StreamReader (response. GetResponseStream (), encoding );
String html = reader. ReadToEnd ();
Return html;
}
}
Catch
{
}
Finally
{
If (response! = Null)
{
Response. Close ();
Response = null;
}
If (reader! = Null)
Reader. Close ();
If (request! = Null)
Request = null;
}
Return string. Empty;