Download and upload data online (i) montaque (original)
Sometimes, in the need of the program, the program requires dynamic updating of the data from the network, such as downloading or uploading enterprise internal data or materials from the data center. Sometimes, want to do some robot-like work, let the program automatically get the intranet or the Internet resources, may be 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:
Member Type description
The current URL address of the BaseURI property
The Downloaddata method downloads data from a URI and returns it as a byte array
The DownloadFile method downloads data from a URI and saves it as a local file
The OpenRead method opens and performs a read operation in the form of a stream
The Openwrite method opens a stream for writing data to the URI
The Uploaddata method uploads data to the URI
The UploadFile method uploads a local file to the development URI
UploadValues method NameValueCollection Send to a resource and return 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.
Public Function Downloaddata (ByVal address as String) as Byte ()
Accepts an argument that the 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
Public Sub DownloadFile (ByVal address As String, ByVal FileName as 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 ("Http://www.csdn.net/images/ad/vsnet_120.gif", "C:\test.gif")
After the successful operation, the local machine's c:\ will have a 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 ()
Uploading data
There is the download must have uploaded, the same corresponding WebClient also have a lot of methods of uploading data, in addition to the corresponding Uploaddata, UploadFile, openwrite outside there is a uploadvalues, UploadValues sends NameValueCollection to the resource and returns a byte array containing any responses that can be used for pages that have a form.
Examples are not written, MSDN Ms-help://ms. Vscc/ms. Msdnvs.2052/cpref/html/frlrfsystemnetwebclientclasstopic.htm all have detailed introduction, here no longer repeat.
Summarize:
This paper briefly introduces the main methods and applications of WebClient, in fact WebClient can do some simple operations, just imagine, if the server asked us to enter the password and user name to access how to do? or other programming details to deal with? That's the WebRequest and WebResponse we're introducing next.
<
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.