WebRequest and WebResponse are two common classes for accessing the Internet. Strictly speaking, WebRequest is an abstract base class. We generally use its response class HttpWebRequest and FileWebRequest. FTP access requires manual registration, for more information, see the insert protocol.
Logically speaking, Net classes includes three layers: Request/response layer, application protocol layer, and transmission layer, which are sequentially expanded from application to network. The WebRequest and WebResponse classes represent the request/response layer. HTTP, TCP, and UDP constitute the application protocol layer, and sockets exist in the transport layer. WebRequest/WebResponse and HTTP can be found in the System. Net namespace, while TCP/UDP and socket are in the System. Net. Sockets namespace, including commonly used UDPClient and TCPCLient.
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 of the. NET Framework for Request/response models used to access Internet data. Applications using this request/response model can request data from the Internet in an unknown protocol? The Protocol is not specific.
These two types provide a general way to access online resources. WebRequest indicates a network request that contains attributes such as RequestURI, Headers, Credentials, and ContentType. The main methods on WebRequest include GetRequestStream, GetResponse, and Their Asynchronous methods Begin/EndGetRequestStream and Begin/EndGetResponse.
GetRequestStream is used to obtain a stream to upload data to the server. GetResponse is used to obtain the response object returned by the server. WebResponse indicates the response received from the server that processes the request. Its key attributes include ContentLength, ContentType, headers, ResponseURI, and Status. The most common method on WebResponse is GetResponseStream, which is used to read (download) data from the server.
When calling WebRequest. GetResponse, the actual network request is usually sent.
This example shows how to obtain a Web page and output its content:
Dim url As New Uri ("http://www.yahoo.com") 'defines a URI