Share via


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.

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. The following example requests a Web page and returns the results in a stream.

WebClient myClient = new WebClient();
Stream response = myClient.OpenRead("https://www.contoso.com/index.htm");
// The stream data is used here.
response.Close();
[Visual Basic]Dim myClient As WebClient = New WebClient()
Dim response As Stream = myClient.OpenRead("https://www.contoso.com/index.htm")
' The stream data is used here.
response.Close()

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, as shown in the following example.

WebRequest req = WebRequest.Create("https://www.contoso.com/");
WebResponse resp = req.GetResponse();
[Visual Basic]Dim req As WebRequest = WebRequest.Create("https://www.contoso.com")
Dim resp As WebResponse = req.GetResponse()

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);
[Visual Basic]Dim sr As StreamReader
sr = New StreamReader(resp.GetResponseStream(), Encoding.ASCII)

See Also

Accessing the Internet