CopyCode The Code is as follows: using system;
Using system. net;
Using system. text;
Using system. Text. regularexpressions;
Class Program
{
// Obtain the HTML content of the webpage and automatically judge Encoding Based on the charset of the webpage
Static string gethtml (string URL)
{
Return gethtml (URL, null );
}
// Obtain the HTML content of the webpage and specify Encoding
Static string gethtml (string URL, encoding)
{
Byte [] Buf = new WebClient (). downloaddata (URL );
If (encoding! = NULL) return encoding. getstring (BUF );
String html = encoding. utf8.getstring (BUF );
Encoding = getencoding (HTML );
If (encoding = NULL | encoding = encoding. utf8) return HTML;
Return encoding. getstring (BUF );
}
// Extract the encoding of the webpage based on the HTML content of the webpage
Static encoding getencoding (string HTML)
{
String Pattern = @"(? I) \ bcharset = (? <Charset> [-a-zA-Z_0-9] + )";
String charset = RegEx. Match (HTML, pattern). Groups ["charset"]. value;
Try {return encoding. getencoding (charset );}
Catch (argumentexception) {return NULL ;}
}
//ProgramPortal
Static void main ()
{
Console. writeline (gethtml (http://www.jb51.net ));
Console. Read ();
}
}