Difference between WebClient and WebRequest

Source: Internet
Author: User

WebClient and HttpWebRequst are two ways to obtain data. Generally, WebClient prefers "download on demand". In fact, it is relatively easy to master, httpWebRequst allows you to set the request header or require more control over the content, which is somewhat similar to the submit in form. Although both are asynchronous request events, WebClient is event-based Asynchronous, while HttpWebRequst is proxy-based asynchronous programming.

WebClient is encapsulated For ease of use, but WebResponse and WebRequest must be used for operations that require a little detail. For example, to control the number of TCP connections, customize the HTTP request header or Post custom data, etc. HTTPWebRequest is a WebRequest dedicated to the HTTP protocol. If you want flexibility to select the latter, select the former for simplicity, 2.0 provides FtpWebResponse... fileWebResponse, which may be used in a wider range.

Example:

For example, WebClient class implementation
PageUrl = UrlText. Text;
WebClient wc = new WebClient ();
Wc. Credentials = CredentialCache. DefaultCredentials;

Byte [] pageData = wc. DownloadData (PageUrl );
ContentHtml. Text = Encoding. Default. GetString (pageData );

Implemented using the WebRequest class
PageUrl = UrlText. Text;
WebRequest request = WebRequest. Create (PageUrl );
WebResponse response = request. GetResponse ();
Stream resStream = response. GetResponseStream ();
StreamReader sr = new StreamReader (resStream, System. Text. Encoding. Default );
ContentHtml. Text = sr. ReadToEnd ();
ResStream. Close ();
Sr. Close ();

What are the differences between the two codes that implement the same function? WebRequest class
Namespace: System. Net

Sends a request to the Uniform Resource Identifier (URI. This is an abstract (MustInherit in Visual Basic) class.
WebRequest is the abstract (MustInherit in Visual Basic) base class of the Request/response model used by. NET Framework to access Internet data. Applications that use this request/response model can request data from the Internet in an unknown way. In this way, the application processes the instance of the WebRequest class, while the Protocol-specific subclass executes the request details.

Requests are sent from an application to a specific URI, such as a Web page on the server. URI identifies the appropriate subclass to be created from a list of WebRequest child instances registered for the application. Registering a WebRequest child 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.

WebClient class:
Provides a public method to send data to the resource identified by the URI and receive data from the resource identified by the URI. This class cannot be inherited.
The WebClient class provides public methods for sending data to any local, Intranet, or Internet Resource identified by the URI and receiving data from these resources.

The WebClient class uses the WebRequest class to provide access to Internet resources. A WebClient instance can access data through any WebRequest child that has been registered with the WebRequest. RegisterPrefix method.
For details, check that msdnnull WebClient does not support timeout settings, which is too bad. Therefore, HttpWebRequest is used to replace WebClient: HttpWebRequest is the parent class of WebClient after all, so POST is troublesome.
Try
{
String valpairs = "";
Valpairs = "c =" + textBox1.Text;
UTF8Encoding encoding = new UTF8Encoding ();
B = encoding. GetBytes (valpairs );
HttpWebRequest request = (HttpWebRequest) WebRequest. Create ("http: // localhost/t/default. aspx ");
Request. Timeout = 1000*5;
Request. Method = "POST ";
Request. ContentType = "application/x-www-form-urlencoded ";
Request. ContentLength = B. Length;

......

}

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.