C # using WebClient and WebRequest to get a comparison of web page source code

Source: Internet
Author: User

A few days ago, the method of obtaining Web page source code with Asp+xmlhttp is analyzed, but in C #, the WebClient class and WebRequest class can be used to obtain the source code of the Web page. The implementation of the two methods is described below.

WebClient class get web page source code

WebClient class

The WebClient class is located under the System.Net namespace, and the WebClient class provides a public way to send data to and receive data from any local, intranet, or Internet resource identified by the URI.

Source

Reference namespaces
Using System.IO;
Using System.Net;
Using System.Text;


Pageurl = "http://www.webkaka.com"; Web pages that need to get source code
WebClient WC = new WebClient (); Create a WebClient instance to provide data to and receive data from the resource identified by the URI
Wc. Credentials = CredentialCache.DefaultCredentials; Gets or sets the network credentials that are used to authenticate requests to Internet resources.

Method One:
Encoding enc = encoding.getencoding ("GB2312"); If it is garbled, change to Utf-8/GB2312
byte[] Pagedata = WC. Downloaddata (Pageurl); Downloads data from a resource and returns a byte array.
Contenthtml.text = Enc. GetString (Pagedata); Output string (HTML code), TextBox control contenthtml for Multiline mode

Method Two:
Code Start **********
Stream resstream = WC. OpenRead (Pageurl); Open a URL as a stream
Encoding enc = encoding.getencoding ("GB2312"); If it is garbled, change to Utf-8/GB2312
StreamReader sr = new StreamReader (RESSTREAM,ENC); Reads the data stream in the specified encoding mode
Contenthtml.text = Sr. ReadToEnd (); Output (HTML code), TextBox control contenthtml for Multiline mode
Resstream.close ();
Code End ********
///
Wc. Dispose ();

WebRequest class get web page source code

WebRequest class

The WebRequest class is the abstract base class for the request/response model in the. NET framework for accessing Internet data. Applications that use the WebRequest class request/response model can request data from the Internet in a protocol-agnostic manner, in which the application processes instances of the WebRequest class, while the protocol-specific subclasses perform the specific details of the request, Requests are sent from the application to a specific URI, such as a Web page on the server. The URI determines the appropriate subclass to be created from a list of WebRequest subclasses registered for the application. Registering a WebRequest subclass is typically done to handle a particular protocol, such as HTTP or FTP, but it can also be registered to handle requests for a path on a particular server or server.

The Create method is most commonly used in the WebRequest class, and the Create method is used to initialize the new WebRequest instance for the specified URI scheme.

Grammar:

public static WebRequest Create
(
String requesturistring
)
Parameters:

requestUriString: The URI that identifies the Internet resource.

Return value: The WebRequest subclass of the specific URI scheme.

Note: The Create method returns the subclass of the WebRequest class that is determined by the runtime as the closest registration match to RequestUri. For example, when a URI that begins with http://is passed in RequestUri, a HttpWebRequest is returned by create. If you pass a URI that begins with file://instead, the Create method returns the FileWebRequest instance. The. NET framework includes support for the http:/and file://URI schemes.


WebResponse class

The WebResponse class is the abstract base class, and the protocol-specific response class derives from the abstract base class. An application can use instances of the WebResponse class to participate in request and response transactions in a protocol-agnostic manner, while the protocol-specific classes derived from the WebResponse class carry the details of the request.

The GetResponse method is most commonly used in the WebResponse class, and the GetResponse method is used to return the response to an Internet request when overridden in a subclass

Grammar:

Public virtual WebResponse GetResponse ()

Return value: Contains the WebResponse of the response to the Internet request.


Source

Reference namespaces
Using System.IO;
Using System.Net;
Using System.Text;

Pageurl = "http://www.webkaka.com"; Web pages that need to get source code
WebRequest request = WebRequest.Create (Pageurl); WebRequest.Create method, returns the WebRequest subclass HttpWebRequest
WebResponse response = Request. GetResponse (); Webrequest.getresponse method that returns the response to an Internet request
Stream Resstream = Response. GetResponseStream (); The WebResponse.GetResponseStream method returns a data stream from an Internet resource.
Encoding enc = encoding.getencoding ("GB2312"); If it is garbled, change to Utf-8/GB2312
StreamReader sr = new StreamReader (Resstream, ENC); Namespace: System.IO. The StreamReader class implements a TextReader (TextReader class that represents a reader that can read a series of sequential characters) so that it reads characters from a byte stream in a specific encoding.
Contenthtml.text = Sr. ReadToEnd (); Output (HTML code), TextBox control contenthtml for Multiline mode
Resstream.close ();
Sr. Close ();

C # using WebClient and WebRequest to get a comparison of web page source code

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.