正在请求数据...Requesting Data

开发在如今 Internet 的分布式操作环境中运行的应用程序需要使用高效易用的方法从所有类型的资源中检索数据。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. 可以通过可插入协议开发使用单一接口从多个 Internet 协议检索数据的应用程序。Pluggable protocols let you develop applications that use a single interface to retrieve data from multiple Internet protocols.

从 Internet 服务器上传和下载数据Uploading and Downloading Data from an Internet Server

对于简单的请求和响应事务,WebClient 类提供最简单的方法来将数据上传到 Internet 服务器或从 Internet 服务器下载数据。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 提供用于上传和下载文件、发送和接收数据流和将数据缓冲区发送到服务器并接收响应的方法。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 使用 WebRequestWebResponse 类来实现与 Internet 资源的实际连接,因此可以使用任何已注册的可插入协议。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.

需要执行更复杂事务的客户端应用程序使用 WebRequest 类及其后代从服务器请求数据。Client applications that need to make more complex transactions request data from servers using the WebRequest class and its descendants. WebRequest 封装连接到服务器、发送请求和接收响应的详细信息。WebRequest encapsulates the details of connecting to the server, sending the request, and receiving the response. WebRequest 是一个抽象类,它定义一组使用可插入协议的所有应用程序均可使用的属性和方法。WebRequest is an abstract class that defines a set of properties and methods that are available to all applications that use pluggable protocols. WebRequest 的后代(如 HttpWebRequest)通过与基础协议一致的方法实现由 WebRequest 定义的属性和方法。Descendants of WebRequest, such as HttpWebRequest, implement the properties and methods defined by WebRequest in a way that is consistent with the underlying protocol.

WebRequest 类使用传递到 Create 方法的 URI 值确定要创建的特定派生类实例,创建 WebRequest 后代特定于协议的实例。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. 应用程序通过 WebRequest.RegisterPrefix 方法注册 WebRequest 后代的构造函数,指示将用于处理请求的后代。Applications indicate which WebRequest descendant should be used to handle a request by registering the descendant's constructor with the WebRequest.RegisterPrefix method.

通过调用 WebRequest 上的 GetResponse 方法,向 Internet 资源发送请求。A request is made to the Internet resource by calling the GetResponse method on the WebRequest. GetResponse 方法从 WebRequest 的属性构造特定于协议的请求、将 TCP 或 UDP 套接字连接到服务器并发送请求。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. 对于将数据发送到服务器的请求(如 HTTP Post 或 FTP Put 请求),WebRequest.GetRequestStream 方法提供要向其发送数据的网络数据流。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.

GetResponse 方法返回特定于协议的 WebResponse,后者与 WebRequest 匹配。The GetResponse method returns a protocol-specific WebResponse that matches the WebRequest.

WebResponse 类也是一个抽象类,它定义使用可插入协议的所有应用程序均可使用的属性和方法。The WebResponse class is also an abstract class that defines properties and methods that are available to all applications that use pluggable protocols. WebResponse 后代为基础协议实现这些属性和方法。WebResponse descendants implement these properties and methods for the underlying protocol. 例如,HttpWebResponse 类为 HTTP 实现 WebResponse 类。The HttpWebResponse class, for example, implements the WebResponse class for HTTP.

向由 WebResponse.GetResponseStream 方法返回的数据流中的应用程序提供服务器返回的数据。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