C # get Web page HTML Source instance

Source: Internet
Author: User

This article describes the C # access to the HTML source code method, share to everyone for your reference. Here's how:

The key code is as follows:


The code is as follows:

<summary>

Get Web page HTML source code

</summary>

<param name= "url" > Links eg:http://www.baidu.com/</param>

<param name= "CharSet" > Encode eg:encoding.utf8</param>

<returns>html Source </returns>

public static string Gethtmlsource (string url, Encoding charset)

{

String _html = String. Empty;

Try

{

HttpWebRequest _request = (HttpWebRequest) webrequest.create (URL);

HttpWebResponse _response = (httpwebresponse) _request. GetResponse ();

using (Stream _stream = _response. GetResponseStream ())

{

using (StreamReader _reader = new StreamReader (_stream, CharSet))

{

_html = _reader. ReadToEnd ();

}

}

}

catch (WebException ex)

{

using (StreamReader sr = new StreamReader (ex. Response.getresponsestream ()))

{

_html = Sr. ReadToEnd ();

}

}

catch (Exception ex)

{

_html = ex. Message;

}

return _html;

}

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # get Web page HTML Source instance

This address: http://www.paobuke.com/develop/c-develop/pbk23440.html






Related content implementation of a state machine SuperSocket Getting Started--telnet server and client request processing detailed parsing C # multithreaded synchronization events and wait handles nine Gongge algorithm based on C # code all equals 4.
C # How to implement Ping method summary C # serialization and deserialization (Serialize,deserialize) Example of C # convert a number to a byte array method using the params keyword of C # to implement a variable number of parameter parameters example

C # get Web page HTML Source instance

Related Article

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.