C #. Net collection class

Source: Internet
Author: User
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. diagnostics;
Using system. Text. regularexpressions;
Using system. IO;
Using system. net;

Namespace capture
{
Class Program
{
Static void main (string [] ARGs)
{
String [] URLs = {
"Http://www.yongfa365.com ",
"Http://www.cbdcn.com ",
"Http://www.csdn.net ",
"Http://www.sina.com ",
"Http://www.tom.com ",
};

String html = "";

Html = caiji. gethtmlsource ("http://www.yongfa365.com", encoding. Default );
Console. Write (HTML );
Console. readkey ();

Html = caiji. gethtmlsource ("http://www.baidu.com /");
Console. Write (HTML );
Console. readkey ();

Html = caiji. gethtmlsource ("http://www.tom.com", "UTF-8 ");
Console. Write (HTML );
Console. readkey ();

Foreach (string URL in URLs)
{
Console. Write (caiji. gethtmlsource (URL ));
Console. readkey ();

}
}
}
}


/// <Summary>
/// Liuyong method collection class
/// </Summary>
Class caiji
{
/// <Summary>
/// Obtain the webpage source code
/// </Summary>
/// <Param name = "url"> webpage address, eg: "http://www.yongfa365.com/" </param>
/// <Param name = "charset"> webpage encoding, eg: "UTF-8" </param>
/// <Returns> return the webpage source file </returns>
Public static string gethtmlsource (string URL, string charset)
{
// Encoding
Encoding nowcharset;
If (charset = "" | charset = NULL)
{
Nowcharset = encoding. default;
}
Else
{
Nowcharset = encoding. getencoding (charset );
}

// Process content
String html = "";
Try
{
// Webrequest mywebrequest = webrequest. Create (URL );
// Webresponse mywebresponse = mywebrequest. getresponse ();
// Stream = mywebresponse. getresponsestream ();
// Streamreader reader = new streamreader (stream, nowcharset );

Httpwebrequest request = (httpwebrequest) webrequest. Create (URL );
Httpwebresponse response = (httpwebresponse) request. getresponse ();
Stream stream = response. getresponsestream ();
Streamreader reader = new streamreader (stream, nowcharset );
Html = reader. readtoend ();
Stream. Close ();
}
Catch (exception E)
{
}
Return HTML;
}

/// <Summary>
/// Obtain the webpage source code
/// </Summary>
/// <Param name = "url"> webpage address, eg: "http://www.yongfa365.com/" </param>
/// <Param name = "charset"> webpage code, eg: encoding. utf8 </param>
/// <Returns> return the webpage source file </returns>
Public static string gethtmlsource (string URL, encoding charset)
{
// Process content
String html = "";
Try
{
Httpwebrequest request = (httpwebrequest) webrequest. Create (URL );
Httpwebresponse response = (httpwebresponse) request. getresponse ();
Stream stream = response. getresponsestream ();
Streamreader reader = new streamreader (stream, charset );
Html = reader. readtoend ();
Stream. Close ();
}
Catch (exception E)
{
}
Return HTML;
}

/// <Summary>
/// Obtain the webpage source code
/// Effective for webpages with Bom, which can be correctly identified by any code
/// </Summary>
/// <Param name = "url"> webpage address, eg: "http://www.yongfa365.com/" </param>
/// <Returns> return the webpage source file </returns>
Public static string gethtmlsource (string URL)
{
// Process content
String html = "";
Try
{
Httpwebrequest request = (httpwebrequest) webrequest. Create (URL );
Httpwebresponse response = (httpwebresponse) request. getresponse ();
Stream stream = response. getresponsestream ();
Streamreader reader = new streamreader (stream, encoding. Default );
Html = reader. readtoend ();
Stream. Close ();
}
Catch (exception E)
{
}
Return HTML;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.