[Post] ASP. NET WebClient usage

Source: Internet
Author: User
Mircsoft provides a public method under the dotnet1.1 framework to send data to the resource identified by the URI and receive data from the resource identified by the URI.
Through this class, you can simulate the access and sending of resources on the Internet by the browser on the basis of the browser.
The WebClient class cannot be inherited. In the dotnet1.1 framework, we have provided two powerful classes: webrequest and webresponse.
Processes The resources indicated by the URI and obtains data. The disadvantage is that the settings are too complex when webrequest and webresponse are used.
It is quite difficult to use. With the current WebClient, WebClient can be understood as webrequest and webresponse.
Encapsulation of collaboration. It makes it easier and more convenient for people to use, and then it has inherent limitations. The cookie/session is missing.
Support: users cannot control whether to enable automatic URL redirection, or lack support for proxy servers. About session/URL redirection control/Proxy
I will introduce you to webrequest/webresponse in the future. The following is a brief introduction.
Webclinet class.

Class Name: WebClient
Namespace System. Net. WebClient

 

Common Constructor
WebClient Constructor InitializationWebClientClass.
Public attributes
Baseaddress Get or setWebClientThe base URI of the request.
Container (fromComponentInheritance) Obtain icontainer, which contains component.
Credentials Obtain or set the network creden。 used to authenticate requests to Internet resources.
Headers Obtains or sets the header name/value pair set associated with the request.
Querystring Gets or sets the set of query name/value pairs associated with the request.
Responseheaders Obtains the header name/value pair set associated with the response.
Site (fromComponentInheritance) Gets or sets the isite of component.
Common Methods
Createobjref (fromMarshalByRefObjectInheritance) Create an object that contains all the information required to generate a proxy for communication with a remote object.
Dispose (fromComponentInheritance) Overloaded. Releases resources occupied by component.
Downloaddata Download data from a resource with a specified Uri.
Downloadfile Download data from a resource with a specified URI to a local file.
Equals (fromObjectInheritance) Overloaded. Determine whether the two object instances are equal.
Gethashcode (fromObjectInheritance) It is used as a hash function of a specific type and is suitable for use in hash algorithms and data structures (such as hash tables.
Getlifetimeservice (fromMarshalByRefObjectInheritance) Retrieves the service objects that control the current lifetime of the instance.
GetType (fromObjectInheritance) Obtain the type of the current instance.
Initializelifetimeservice (fromMarshalByRefObjectInheritance) Gets the lifetime service object that controls the lifetime policy of this instance.
Openread Open a readable stream for data downloaded from resources with a specified Uri.
Openwrite Overloaded. Open a stream to write data to resources with the specified Uri.
Tostring (fromObjectInheritance) Returns the String of the current Object.
UploadData Overloaded. Upload the data buffer to a resource with the specified URI.
UploadFile Overloaded. Upload a local file to a resource with the specified URI.
UploadValues Overloaded. Upload the name/value set to a resource with the specified URI.

From the preceding table, we can see that WebClient provides four methods to upload data to resources:

  • OpenWrite returns a Stream used to send data to resources.
  • Uploaddata sends the byte array to the resource and returns the byte array containing any response.
  • Uploadfile sends the local file to the resource and returns a byte array containing any response.
  • Uploadvalues sends the namevaluecollection to the resource and returns a byte array containing any response.

In addition, WebClient provides three methods to download data from resources:

  • Downloaddata downloads data from the resource and returns a byte array.
  • Downloadfile downloads data from a resource to a local file.
  • Openread returns data from a resource in the form of stream.

Below we will use a simple application to test the simplest usage of WebClient as the end of this section to give you a preliminary understanding of WebClient.

Example 1: Use WebClient to access the homepage of the blog Garden

First, we use HttpLook to analyze this access. In order to facilitate the analysis, I especially removed the browser's access to images so that we can see more simple analysis results.

We can see that we initiated four resource requests throughout the process, the first of which was to visit the blog homepage.
The second visit is the style sheet file, and the third and fourth accesses are js scripts.
Click the first item to view the http header information about the resource access. The so-called http header is invisible elements transmitted by browsers and remote servers that we cannot see.

1get/http/ 1.1
2 accept: image/GIF, image/X-xbitmap, image/JPEG, image/pjpeg, application/X-Shockwave-flash, application/vnd. MS-Excel, application/vnd. MS-PowerPoint, application/MSWord ,*/*
3accept-language: ZH-CN
4ua-cpu: x86
5accept-encoding: gzip, deflate
6user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; sv1;. Net CLR 1.1.4322;. Net CLR 2.0.50727)
7 HOST: www.cnblogs.com
8 connection: keep-alive
9 COOKIE:. dottextcookie = (hidden)

The http information contains the browser access process. Where
The first line: the relative path of the request address and the Protocol Relative Path is/The Protocol uses http1.1
Row 2: indicates the requested resource type.
Row 3: our language is simplified Chinese.
Row 4: The CPU structure we use. This http header is not found on general web pages. Is it estimated that it was a blog Park survey ??
Line 5: Mark is compressed html encoding in gzip mode for transmission. Only some browsers support gzip decompression using this method to pass text. Because we
The program to be written does not have the ability to decompress gzi, so we do not consider using this method to send requests.
Row 6: browser description
Row 7: current host address
Row 8: Connection Request status
Row 9: cookies
 
I used WebClient in the newly created application to implement this process.

I will explain the key implementations below

1 WebClient _ client = new WebClient ();
2 _ client. baseaddress = "http://www.cnblogs.com ";
3 _ client. headers. add ("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd. ms-excel, application/vnd. ms-powerpoint, application/msword ,*/*");
4 _ client. Headers. Add ("Accept-Language", "zh-cn ");
5 _ client. Headers. Add ("UA-CPU", "x86 ");
6 // _ client. Headers. Add ("Accept-Encoding", "gzip, deflate ");
7 _ client. headers. add ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1 ;. net clr 1.1.4322 ;. net clr 2.0.50727 )");
8 System. IO. Stream objStream = _ client. OpenRead ("/");
9 System. IO. StreamReader _ read = new System. IO. StreamReader (objStream, System. Text. Encoding. UTF8 );
10 textBox1.Text = _ read. ReadToEnd ();

Line 1: Create a WebClient instance _ client
Line 2 ~ Row 7: place the Http header captured above to the _ client instance. Note that line 6 is commented out. Because our program cannot perform gzip decoding, if this request is made
The obtained resources may not be decoded. Of course, we can add the gzip processing module to the program.
Row 8: Use _ client. OpenRead (string URI) to obtain the Stream of online resources.
Row 9: Use StreamReader to parse Stream using the encoding method we need. UTF8 is used here. Different decoding methods such as Default can be used for different websites.
Row 10: Put the decoded content in textBox1 to display it.

Let's get a general introduction to WebClient. In the future, we will introduce various attributes and methods of WebClient.
Using WebClient as a resource thief is actually very simple, so you must anti-leech !!!

Source: http://www.moodread.cn/article.asp? Id = 75

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.