The method needs to pass the encoding of the target page, 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 tell in the comments. Thank you!
///
Get 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;