对可插入协议进行编程Programming Pluggable Protocols

WebRequestWebResponse 抽象类为可插入协议提供了基础。The abstract WebRequest and WebResponse classes provide the base for pluggable protocols. 通过从 WebRequestWebResponse 派生协议特定的类,应用程序可以请求 Internet 资源中的数据并读取响应而无需指定所使用的协议。By deriving protocol-specific classes from WebRequest and WebResponse, an application can request data from an Internet resource and read the response without specifying the protocol being used.

在创建协议特定的 WebRequest 前,必须首先注册其 Create 方法。Before you can create a protocol-specific WebRequest, you must register its Create method. 使用WebRequest 的静态 RegisterPrefix(String, IWebRequestCreate) 方法注册一个 WebRequest 子代来处理一组请求(包括对某一特定 Internet 方案的请求、对某个方案和服务器的请求或对某个方案、服务器和路径的请求)。Use the static RegisterPrefix(String, IWebRequestCreate) method of WebRequest to register a WebRequest descendant to handle a set of requests to a particular Internet scheme, to a scheme and server, or to a scheme, server, and path.

在大多数情况下,可以使用 WebRequest 类的方法和属性发送和接收数据。In most cases you will be able to send and receive data using the methods and properties of the WebRequest class. 但是,如果需要访问协议特定的属性,则可以将 WebRequest 的类型转换成特定的派生类实例。However, if you need to access protocol-specific properties, you can typecast a WebRequest to a specific derived-class instance.

要利用可插入协议,WebRequest 子代必须提供一个默认的“请求并响应”事务,该事务不要求设置协议特定的属性。To take advantage of pluggable protocols, your WebRequest descendants must provide a default request-and-response transaction that does not require protocol-specific properties to be set. 例如 HttpWebRequest 类实现了 HTTP 的 WebRequest 类,默认情况下提供 GET 请求并返回包含从 Web 服务器返回的流的 HttpWebResponseFor example, the HttpWebRequest class, which implements the WebRequest class for HTTP, provides a GET request by default and returns an HttpWebResponse that contains the stream returned from the Web server.

请参阅See also