Download and upload data online (2)

zhaozj2021-02-17  58

Download and upload data online (2)

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

The last introduced some of WebClient's download and uploading data, today he focuses on WebRequest and WebResponse we have access to the Internet commonly used class, strictly said that WebRequest is an abstract base class, we generally use its derived class HttpWebRequest and FileWebRequest , FTP access requires manual registration, and specifically looks at the insert protocol.

From a logical point of view, NET Classes contains three layers: request / response layer, application protocol layer, and transport layer, sequentially expand, from the application to the network. WebRequest and WebResponse class represent the request / response layer. HTTP, TCP, and UDP classes constitute an application protocol layer, and sockets are present in the transport layer. WebRequest / WebResponse and HTTP can be found in the System.net namespace, while TCP / UDP and sockets are in the System.Net.Sockets namespace, including NDPCLIENT, TCPClient, which is also commonly used.

WebRequest and WebResponse (request / response model)

The request / response model is located at the top, providing a simple way to access resources on the Web. WebRequest is an abstract base class for accessing Internet data for accessing Internet data. Application using this request / response model can request data from the Internet in a way that the protocol is unknown. What do you mean? It is not specific to the agreement.

These two classes provide a general way to access online resources. WebRequest represents a network request, including properties such as Requesturi, Headers, Credentials, and ContentType. The main method on WebRequest has getRequestStream, GetResponse, and their asynchronous method Begin / EndgetRequestStream and Begin / EndgetResponse. GetRequestStream is used to get the stream to upload the data to the server. GetResponse is used to get the response object returned by the server. WebResponse represents the response from the server that is processed by the request. Its key attribute has ContentLength, ContentType, Headers, Responseuri, and STATUS. The most common method on WebResponse is GetResponseSteream, which is used to read (download) data from the server.

When you call WebRequest.getResponse, an actual network request is usually issued.

This example shows how to get a web page and output it:

DIM URL AS New URI ("http://www.yahoo.com") 'Defines a URI

DIM Req as WebRequest

Req = WebRequest.create (URL)

Dim Resp as WebResponse

Try

Resp = Req.getResponse 'gets a response

Catch EXC AS EXCEPTION

Msgbox (Exc.Message)

END TRY

DIM NetStream As StreamReader

NetStream = New StreamReader (Resp.getResponsestream)

Debug.writeLine (NetStream.ReadToend)

Of course, the above example is just a simple operation, and a streaming data can be returned. The following is fully excavated (compared to WebClient). Inserted protocol

When an application only uses WebRequest and WebResponse classes, you can "insert" and use new protocols without any code to modify applications. Register a URI mode with WebRequestFactory inserts protocol support within the duration of the program. The registration is done by calling the WebRequestFactory.register () method. For HTTP protocols, this method is called from internal, so it is registered by default; despite this, other protocols can be implemented and registered in the future. Of course, this model is not satisfactory for all situations due to huge number of protocols on the Internet. The protocols of those "rap" or additional exclusive requests / response models may be better executed in the TCP or UDP classes, or in some cases better execution in the Sockets class. We can use HTTPWebRequest on HTTP.

Hereinafter, several specific issues:

1. Program block

The program block is useful when the application needs to send or receive data, and the accurate size of the data is not known when the download / upload begins. Using program block technology is most common when the data discussed based on other applications or server logic is being created. To send a block data, WebRequest should be converted to HttpWebRequest and set the property of httpwebrequest.sendchunked to true. If you naturally use HttpWebRequest to naturally :)

2. HTTP pipeline technology

Pipe technology is a feature of HTTP 1.1, which allows NET Classes to send multiple HTTP requests to the rear server through a persistent connection without waiting for a next request. Response from the server. This will significantly affect performance because applications from the server are not blocked and waited for a particular resource (probably a very time consuming operation on the server, such as database lookup).

3. Authentication

Net Classes supports a variety of client authentication mechanisms, including "Summary", "Basic", Kerberos, NTLM, and "Custom". Authentication is achieved by setting the WebRequest.credentials object before making a request. In "Summary" and "Basic" case, the username and password will be specified. For NTLM or Kerberos, the Windows security mechanism is used, and the Credential object may be set as a combination of username, password, and domain, or can request the system default value.

4. Agent support

HTTP proxy support in NET Classes can be controlled on each request, or set it in a glib to set it for the application of the application. This seems to verify that proxy is free;)

to sum up:

Overview another way to access the Internet, WebResponse WebRequest, and some of its advantages. Specific example and illustration Reference: http://www.microsoft.com/china/msdn/library/techart/pdc_websvc.asp is a very good article.

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

New Post(0)