C # comparison of Web source code obtained using WebClient and WebRequest

Source: Internet
Author: User

In C #, you can use the WebClient class and WebRequest class to obtain the webpage source code. The following describes the implementation of these two methods.
WebClient class to obtain webpage source code
WebClient class
The WebClient class is located in the System. Net namespace. The WebClient class provides public methods to send data to any local, Intranet, or Internet Resource identified by the URI and receive data from these resources.
Source code
/// Reference the namespace
Using System. IO;
Using System. Net;
Using System. Text;
 
 
PageUrl = "http://www.webkaka.com"; // a page that requires source code
WebClient wc = new WebClient (); // create a WebClient instance to send data to the resource identified by the URI and receive data from the resource identified by the URI
Wc. Credentials = CredentialCache. DefaultCredentials; // gets or sets the network creden。 used to authenticate requests to Internet resources.
 
/// Method 1:
Encoding enc = Encoding. GetEncoding ("GB2312"); // if it is garbled, change it to UTF-8/GB2312
Byte [] pageData = wc. DownloadData (PageUrl); // download data from the resource and return a Byte array.
ContentHtml. Text = enc. GetString (pageData); // output string (HTML code). ContentHtml is a TextBox Control in Multiline mode.
 
/// Method 2:
/// **************** Code start **********
/// Stream resStream = wc. OpenRead (PageUrl); // open the URL as a Stream
/// Encoding enc = Encoding. GetEncoding ("GB2312"); // if it is garbled, change it to UTF-8/GB2312
/// StreamReader sr = new StreamReader (resStream, enc); // read data streams in the specified encoding Mode
/// ContentHtml. Text = sr. ReadToEnd (); // output (HTML code). ContentHtml is a TextBox Control in Multiline mode.
/// ResStream. Close ();
/// ***************** Code end ********
///
Wc. Dispose ();
WebRequest class to obtain webpage source code
WebRequest class
The WebRequest class is the abstract base class of the "Request/response" Model in. NET Framework. It is used to access Internet data. Applications that use the WebRequest request/response model can request data from the Internet in an unknown way. In this way, applications process instances of the WebRequest class, A specific sub-class of the Protocol executes the specific details of the request. The request is sent from the application to a specific URI, such as a webpage on the server. URI identifies the appropriate subclass to be created from the list of WebRequest subclass registered for the application. Registering a WebRequest subclass is usually used to process a specific protocol (such as HTTP or FTP), but it can also be registered to process requests to paths on a specific server or server.
The most common WebRequest method is the Create method. The Create method is used to initialize a new WebRequest instance for the specified URI scheme.
Syntax:
Public static WebRequest Create
(
String requestUriString
)
Parameters:
RequestUriString: The URI that identifies Internet resources.
Returned value: the WebRequest subclass of a specific URI scheme.
Note: The Create method returns the subclass of the WebRequest class determined during runtime as the registration match closest to requestUri. For example, when a URI starting with http: // is passed in requestUri, Create returns an HttpWebRequest. If a URI starting with file: // is passed, the Create method returns the FileWebRequest instance .. NET Framework supports http: // and file: // URI solutions.
WebResponse class
The WebResponse class is an abstract base class. Protocol-specific response classes are derived from this abstract base class. Applications can use instances of the WebResponse class to participate in request and response transactions in an unknown way, while protocol-specific classes derived from the WebResponse class carry request details.
The GetResponse method is the most commonly used method in the WebResponse class. The GetResponse method is used to return a response to an Internet request when it is overwritten in the subclass.
Syntax:
Public virtual WebResponse GetResponse ()
Returned value: contains the WebResponse response to the Internet request.
Source code
/// Reference the namespace
Using System. IO;
Using System. Net;
Using System. Text;
 
PageUrl = "http://www.bkjia.com"; // a page that requires source code
WebRequest request = WebRequest. Create (PageUrl); // WebRequest. Create method, return the subclass HttpWebRequest of WebRequest
WebResponse response = request. GetResponse (); // WebRequest. GetResponse method, returns the response to the Internet request
Stream resStream = response. GetResponseStream (); // The WebResponse. GetResponseStream method returns data streams from Internet resources.
Encoding enc = Encoding. GetEncoding ("GB2312"); // if it is garbled, change it to UTF-8/GB2312
StreamReader sr = new StreamReader (resStream, enc); // namespace: System. IO. The StreamReader class implements a TextReader (TextReader class, indicating that the reader can read the continuous character series) so that it can read characters from the byte stream with a specific encoding.
ContentHtml. Text = sr. ReadToEnd (); // output (HTML code). ContentHtml is a TextBox Control in Multiline mode.
ResStream. Close ();
Sr. Close ();

From Shine's holy heaven-Min Chen 〃

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.