Data | download sometimes, in the need of the program, the program requires a dynamic update of the data from the network, such as downloading or uploading enterprise internal data or data from the data center; sometimes, you want to do some kind of robot work, so that programs automatically get the intranet or Internet resources, Maybe it's news, pictures, and what you want ... This time need to compile some small program. today I would mainly introduce you to the. NET how to obtain the data on the network, of course can be LAN, even local file system. Using the WebClient class, it's a breeze! about
WebClient
: in MSDN, this describes the WebClient class:
"provides a public way to send data to and receive data from a resource identified by a URI", by default, the. NET framework supports URIs that begin with http:, https: and file: Scenario identifiers. Does it encapsulate a lot of the process we want to implement? Oh. Take a look at its main members:
| Members |
Type |
Describe |
| BaseURI |
Property |
The current URL address |
| Downloaddata |
Method |
Downloads data from a URI, returning as an array of bytes |
| DownloadFile |
Method |
Download data from a URI, save as a local file |
| OpenRead |
Method |
Open and perform a read operation as a stream |
| Openwrite |
Method |
Opens a stream for writing data to the URI |
| Uploaddata |
Method |
Uploading data to a URI |
| UploadFile |
Method |
Upload a local file to the development URI |
| UploadValues |
Method |
NameValueCollection sends to a resource and returns an array of bytes containing any response |
Take a look at how to
download files or data: WebClient provides about three ways to download data from the Web: 1.
Downloaddata downloads data from a resource and returns a byte array.
address String Byte()
accepts an argument thatthe address is the URI from which the data is downloaded . Note that the return is a byte array, which I have mentioned many times in previous articles, and we can easily convert to the format we need.
Look at a code:
Dim WC as New System.Net.WebClient () ' network-related classes are generally under system.net
Dim html as String = Encoding.ASCII.GetString (WC. Downloaddata ("Http:www.csdn.net"))
Debug.WriteLine (HTML)
you'll get a very long string, which is actually the source code for the first page of CSDN. 2.
DownloadFile downloads data to a local file from a resource with the specified URI
address string FileName string)
Address : The URI from which data is downloaded. FileName: The name of the local file to receive the data.
It is also simple to use:
Dim WC as New System.Net.WebClient ()
Wc. DownloadFile ("/xrssfile/2006-12/26/2006122602354452.gif")
after successfully running, the local machine's C:\ will have one more small picture, is vs.net 4CD advertisement.
3. OpenRead opens a readable stream for data downloaded from a resource with the specified URI. Public Function OpenRead (ByVal Address as String ) as Stream Parameters the URI from which the address downloads data. familiar with the concept of flow? If you are not familiar with the article, look at my previous articles, very basic operations have. The following example opens the resource identified by uristring and displays the results on the system console. Note that the Stream returned by OpenRead will be closed after reading the data.
Dim mywebclient as New System.Net.WebClient ()
Dim uristring as string= "Http://www.csdn.net"
Console.WriteLine ("Accessing {0} ...", uristring)
Dim MyStream as Stream = Mywebclient.openread (uristring)
Console.WriteLine (ControlChars.Cr + "displaying Data:" + controlchars.cr)
Dim SR as New StreamReader (MyStream)
Console.WriteLine (Sr. ReadToEnd ())
Mystream.close ()