Download and upload data online (ii) montaque (original)

Source: Internet
Author: User
Tags abstract contains insert ftp access
Upload | data | Download and upload data online (ii) montaque (original)

The last time I introduced the WebClient of some ways to download and upload data, today focus on the WebRequest and WebResponse two kinds of our access to the Internet commonly used classes, strictly speaking, WebRequest is an abstract base class, We generally use its derivative classes HttpWebRequest and filewebrequest,ftp access need to manually register, specifically look at the following insert protocol.

Logically, the Net classes contains three tiers: the request/Response layer, the Application protocol layer, and the transport layer, expanded sequentially from the application to the network. The WebRequest and WebResponse classes represent the request/response layer. HTTP, TCP, and UDP classes form the application protocol layer, while sockets exist in the transport layer. Webrequest/webresponse and HTTP can be found in the System.Net namespace, while tcp/udp and sockets are in System.Net.Sockets namespaces, including also commonly used udpclient, TcpClient.

WebRequest and WebResponse (Request/response model)

The request/response model is at the top, providing an easy way to access resources on the Web. WebRequest is an abstract base class for the. NET Framework's request/response model for accessing Internet data. Applications that use this request/Response model can request data from the Internet in a protocol-agnostic way, what do you mean? Is that the agreement is not specific.

These two types provide a general way to access resources on the Web. WebRequest represents a network request that contains attributes such as RequestUri, Headers, Credentials, and ContentType. The main methods on WebRequest are GetRequestStream, GetResponse, and their asynchronous methods Begin/endgetrequeststream and Begin/endgetresponse. GetRequestStream is used to get the stream to upload data to the server. GetResponse is used to get the response object returned by the server. WebResponse represents the response received from the server that processed the request. Its key attributes are ContentLength, ContentType, headers, ResponseUri, and Status. The most commonly used method on WebResponse is GetResponseStream, which is used to read (download) data from the server.

When a call is made to Webrequest.getresponse, the actual network request is usually issued.

This example shows how to get a Web page and output its content:

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 ' Get 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 that can return a stream of data. Here's a full scoop on their advantages (compared to WebClient).

Pluggable protocols

When an application uses only WebRequest and WebResponse classes, you can "insert" and use new protocols without modifying any code for the application. Registers a URI pattern with webrequestfactory to insert protocol support during the duration of the program. Registration is done by calling the Webrequestfactory.register () method. For the HTTP protocol, this method is called from the inside, so it is registered by default, however, any number of other protocols can be implemented and registered in the future. Of course, because of the large number of protocols on the Internet, this model is not ideal for all situations. Those "rap" or otherwise reject the request/response model protocols may be better executed in TCP or UDP classes, or in some cases better executed in the Sockets class. We can use HttpWebRequest for HTTP-related operations.

A few specific questions are described below:

1. Program chunking

Program chunking is useful when the application needs to send or receive data, and the exact size of the data is unknown at the start of the download/upload. The use of program chunking is most common when the data being discussed is being created based on other applications or server logic. To send chunking data, you should convert WebRequest to HttpWebRequest and set the Httpwebrequest.sendchunked property to True. If the direct use of HttpWebRequest natural will be free:

2. HTTP Pipeline Technology

Pipeline technology is a feature of HTTP 1.1 that allows Net classes to send multiple HTTP requests over a persistent connection to the back-end server without waiting for a response from the server before the next request is made. This can significantly affect performance because applications that request multiple resources from the server are not blocked from waiting for a particular resource (possibly a very time-consuming operation on the server, such as a database lookup).

3. Authentication
Net classes supports a variety of client authentication mechanisms, including "Summary", "Basic", Kerberos, NTLM, and "customization." Authentication is achieved by setting the Webrequest.credentials object before making the request. In the summary and basic cases, the user name and password are specified. For NTLM or Kerberos, Windows security is used, and the credential object can be set as a combination of user name, password, and domain, or you can request the use of system defaults.

4. Proxy support
HTTP proxy support in Net classes can be controlled on a per-request basis, or it can be set globally for the lifetime of the application. This seems to verify that the proxy is free;

Summarize:

Outlines another way to access the Internet, WebResponse WebRequest, and some of the advantages of using it. Concrete examples and graphical references: Http://www.microsoft.com/china/msdn/library/techart/pdc_websvc.ASP is a very good 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.