Download and upload data online (1)

zhaozj2021-02-17  55

Download and upload data online (1)

Download & Upload Files or Data in vb.net by Montaque

Sometimes, the needs of the program, the program requires dynamic update data from the network, the most common, such as downloading or uploading the internal data or information from the data center; sometimes, want to do some similar robots, let the program automatically get intranet Also or the resources on the Internet, may be news, pictures, and what you want to get ... At this time, you need to edit some small programs.

Today I mainly introduce the data on the network in .NET, it is of course possible to be a local area network, or even a local file system. Use the WebClient class, easy to be in charge!

About WebClient:

In MSDN, this describes the WebClient class:

"Provides public methods for sending data to URI identifier and resource receiving data from URI identifier", by default, .NET framework supports URI starting with http:, https:, and file: scheme identifier. Is it a lot of the process we want to implement? Ha ha. Look at it's main member:

member

Types of

description

Baseuri

Attributes

Current URL address

Downloaddata

method

Download data from a URI, return in the form of byte arrays

Downloadfile

method

Download data from a URI, save it as a local file

OpenRead

method

Open and perform read operations in the form of flow

OpenWrite

method

Open a stream to write data to URI

UploadData

method

Upload data to URI

UploadFile

method

Upload a local file to the developed URI

UploadValues

method

NameValueCollection Sends to the resource and returns an array of bytes that contain any response

Take a look at how to download files or data:

WebClient provides more than three ways to download data from the Internet:

1. Downloaddata

Download data from the resource and return to the byte array.

Public Function Downloaddata (Byval Address As String) AS Byte ()

Accept a parameter, address is the URI downloading data from it. Note that the return is a byte array, I mentioned many times in the previous article, we can easily convert to our format.

Look at one code:

DIM WC as new system.net.WebClient () 'The class related to the network is generally under system.net.

DIM HTML AS STRING = Encoding.ascii.getstring (wc.downloaddata ("http: www.9cbs.net")))))

Debug.writeLine (HTML)

You will get a very string, actually the source code of 9CBS first page.

2.

Downloadfile

Download data to local files from resources with the specified URI

Public Sub Downloadfile (Byval FileName As String)

Address: Uri downloaded from the data from it.

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

Use is also very simple:

DIM WC as new system.net.webclient ()

wc.downloadfile ("http://www.9cbs.net/images/ad/vsnet_120.gif", "c: /test.gif")

After successful operation, the local machine's C: / will have a small picture, which is the advertisement of VS.NET 4CD. 3. OpenRead

Open a readable stream for data downloaded from the resource with the specified URI.

Public Function OpenRead (Byval Address As String) AS Stream

parameter

Address downloads the URI of the data.

Is the concept of flow familiar? If you don't know, look at my previous article, very basic operation.

The following example opens the resources identified by the uristring and display the result on the system console. Note that the stream returned by OpenRead will be turned off after reading the data.

Dim MyWebClient as new system.net.webclient ()

DIM uristring as string = "http://www.9cbs.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 ()

upload data

There must be uploaded, the same corresponding WebClient also has many ways to upload data, in addition to the corresponding UPLOADATA, UPLOADFILE, OpenWrite, UPLOADVALUES Send NameValueCollection to the resource and returns an array of bytes that contain any response, Can be used for web pages with a form.

The example is not written, MSDN MS-Help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemNetWebClientClasstopic.htm has a detailed introduction, and details are not described herein.

to sum up:

Briefly introduce the main methods and applications of WebClient, in fact, WebClient can do some relatively simple operations. What should I do if the server requires us to enter a password and a username to access? Or do other programming details to handle? That is the WebRequest and WebResponse you introduced next time.

转载请注明原文地址:https://www.9cbs.com/read-30878.html

New Post(0)