Requesting Data
Developing applications that run in the distributed operating environment of today's Internet requires an efficient, easy-to-use method for retrieving data from resources of all types. Pluggable protocols let you develop applications that use a single interface to retrieve data from multiple Internet protocols.
Uploading and Downloading Data from an Internet Server
For simple request and response transactions, the WebClient class provides the easiest method for uploading data to or downloading data from an Internet server. WebClient provides methods for uploading and downloading files, sending and receiving streams, and sending a data buffer to the server and receiving a response. WebClient uses the WebRequest and WebResponse classes to make the actual connections to the Internet resource, so any registered pluggable protocol is available for use.
Client applications that need to make more complex transactions request data from servers using the WebRequest class and its descendants. WebRequest encapsulates the details of connecting to the server, sending the request, and receiving the response. WebRequest is an abstract class that defines a set of properties and methods that are available to all applications that use pluggable protocols. Descendants of WebRequest, such as HttpWebRequest, implement the properties and methods defined by WebRequest in a way that is consistent with the underlying protocol.
The WebRequest class creates protocol-specific instances of WebRequest descendants, using the value of the URI passed to its Create method to determine the specific derived-class instance to create. Applications indicate which WebRequest descendant should be used to handle a request by registering the descendant's constructor with the WebRequest..::.RegisterPrefix method.
A request is made to the Internet resource by calling the GetResponse method on the WebRequest. The GetResponse method constructs the protocol-specific request from the properties of the WebRequest, makes the TCP or UDP socket connection to the server, and sends the request. For requests that send data to the server, such as HTTP Post or FTP Put requests, the WebRequest..::.GetRequestStream method provides a network stream in which to send the data.
The GetResponse method returns a protocol-specific WebResponse that matches the WebRequest.
The WebResponse class is also an abstract class that defines properties and methods that are available to all applications that use pluggable protocols. WebResponse descendants implement these properties and methods for the underlying protocol. The HttpWebResponse class, for example, implements the WebResponse class for HTTP.
The data returned by the server is presented to the application in the stream returned by the WebResponse..::.GetResponseStream method. You can use this stream like any other, as shown in the following example.
StreamReader sr =
new StreamReader(resp.GetResponseStream(), Encoding.ASCII);
Dim sr As StreamReader
sr = New StreamReader(resp.GetResponseStream(), Encoding.ASCII)
See Also
Concepts
How to: Request a Web Page and Retrieve the Results as a Stream
How to: Retrieve a Protocol-Specific WebResponse that Matches a WebRequest