WebRequest class, WebResponse class and WebClient class

Source: Internet
Author: User
Tags abstract html page resource

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 way, in which case the application handles instances of the WebRequest class, while the protocol-specific subclass executes 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 create from a list of WebRequest subclasses registered for the application. Registering a WebRequest subclass is typically intended to handle a particular protocol (such as HTTP or FTP), but it can also be registered to handle requests for a specific server or path on a server.

The most commonly used in the WebRequest class is the Create method, which is described in detail below.

The Create method is used to initialize a new WebRequest instance for the specified URI scheme.

Grammar:

public static WebRequest Create
(
  string requestUriString
)
Parameters:

requestUriString: Identifies the URI of an Internet resource.

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

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

WebResponse class

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

The most commonly used in the WebResponse class is the GetResponse method, which is described in detail below.

The GetResponse method is used to return a response to an Internet request when overridden in a subclass

Grammar:

Public virtual WebResponse GetResponse ()

Return value: The WebResponse that contains the response to an Internet request.

WebClient class

The WebClient class provides public methods for sending data to and receiving data from any local, intranet, or Internet resource identified by the URI. In the WebClient class, the most commonly used is the DownloadFile method, which is described in detail below.

DownloadFile Method: Downloads A resource with the specified URI to a local file.

Grammar:

public void DownloadFile
(
  string address,
  string fileName
)
Parameters:

Address: The URI from which data is downloaded.

Filename: The name of the local file to receive the data.

In C #, the download of the file can be implemented through the HttpWebResponse class and the WebRequest class, and this example will implement the download of the HTML Web page. To run this instance, click the Get web page Content button on the form to download the home page for any Web site, and you can save it to a specific location through the WebClient class. Example run results when you download an HTML page as shown in Figure * * and figure * * When you want to download the HTML page to your local hard disk.

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.