WebRequest 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向统一资源标识符(URI)发出请求。 这是一个 abstract 类。
public ref class WebRequest abstract
	public ref class WebRequest abstract : MarshalByRefObject, System::Runtime::Serialization::ISerializable
	public abstract class WebRequest
	public abstract class WebRequest : MarshalByRefObject, System.Runtime.Serialization.ISerializable
	[System.Serializable]
public abstract class WebRequest : MarshalByRefObject, System.Runtime.Serialization.ISerializable
	type WebRequest = class
	type WebRequest = class
    inherit MarshalByRefObject
    interface ISerializable
	[<System.Serializable>]
type WebRequest = class
    inherit MarshalByRefObject
    interface ISerializable
	Public MustInherit Class WebRequest
	Public MustInherit Class WebRequest
Inherits MarshalByRefObject
Implements ISerializable
		- 继承
 - 
				WebRequest
 
- 继承
 
- 派生
 
- 属性
 
- 实现
 
示例
以下示例演示如何创建 WebRequest 实例并返回响应。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Create a request for the URL. 		
            WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            // Display the status.
            Console.WriteLine (response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Cleanup the streams and the response.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
    Public Class WebRequestGetExample
        Public Shared Sub Main()
            ' Create a request for the URL. 		
            Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
            ' If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials
            ' Get the response.
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            ' Display the status.
            Console.WriteLine(response.StatusDescription)
            ' Get the stream containing content returned by the server.
            Dim dataStream As Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)
            ' Cleanup the streams and the response.
            reader.Close()
            dataStream.Close()
            response.Close()
        End Sub
    End Class
End Namespace
	注解
谨慎
              WebRequest、HttpWebRequest、ServicePoint和 WebClient 已过时,不应将其用于新开发。 请改用 HttpClient。
              WebRequest 是 abstract 基类。用于从 Internet 访问数据的请求/响应模型。 使用请求/响应模型的应用程序可以采用与协议无关的方式从 Internet 请求数据,应用程序可在协议特定的子代类执行请求的详细信息时处理 WebRequest 类的实例。
请求从应用程序发送到特定的 URI,例如服务器上的网页。 URI 确定从为应用程序注册的 WebRequest 后代列表创建的正确后代类。 WebRequest 后代通常注册为处理特定协议(例如 HTTP 或 FTP),但可以注册以处理对服务器上的特定服务器或路径的请求。
当访问 Internet 资源时出错时,WebRequest 类将引发 WebException。 Status 属性是指示错误源的 WebExceptionStatus 值之一。 当 StatusWebExceptionStatus.ProtocolError时,Response 属性包含从 Internet 资源收到的 WebResponse。
由于 WebRequest 类是 abstract 类,因此运行时 WebRequest 实例的实际行为由 Create 方法返回的子代类决定。 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequest 和 FileWebRequest。
注意
使用 Create 方法初始化新的 WebRequest 实例。 请勿使用 WebRequest 构造函数。
注意
如果创建 WebRequest 对象的应用程序使用 Normal 用户的凭据运行,则应用程序将无法访问本地计算机存储中安装的证书,除非已向用户显式授予执行此操作的权限。
实施者说明
从 WebRequest继承时,必须重写以下成员:Method、RequestUri、Headers、ContentLength、ContentType、Credentials、PreAuthenticate、GetRequestStream()、BeginGetRequestStream(AsyncCallback, Object)、EndGetRequestStream(IAsyncResult)、GetResponse()、BeginGetResponse(AsyncCallback, Object)和 EndGetResponse(IAsyncResult)。 此外,必须提供 IWebRequestCreate 接口的实现,该接口定义调用 Create(Uri)时使用的 Create(Uri) 方法。 必须使用 RegisterPrefix(String, IWebRequestCreate) 方法或配置文件注册实现 IWebRequestCreate 接口的类。
构造函数
| WebRequest() | 
			 
				已过时.
			 
		初始化 WebRequest 类的新实例。  | 
        	
| WebRequest(SerializationInfo, StreamingContext) | 
			 
				已过时.
			 
		从 SerializationInfo 和 StreamingContext 类的指定实例初始化 WebRequest 类的新实例。  | 
        	
属性
| AuthenticationLevel | 
		 获取或设置一个值,该值指示用于此请求的身份验证和模拟级别。  | 
        	
| CachePolicy | 
		 获取或设置此请求的缓存策略。  | 
        	
| ConnectionGroupName | 
		 在后代类中重写时,获取或设置请求的连接组的名称。  | 
        	
| ContentLength | 
		 在后代类中重写时,获取或设置要发送的请求数据的内容长度。  | 
        	
| ContentType | 
		 在后代类中重写时,获取或设置要发送的请求数据的内容类型。  | 
        	
| CreatorInstance | 
			 
				已过时.
			 
		在后代类中重写时,获取派生自 IWebRequestCreate 类的工厂对象,该类用于创建实例化 WebRequest,以便向指定的 URI 发出请求。  | 
        	
| Credentials | 
		 在后代类中重写时,获取或设置用于使用 Internet 资源对请求进行身份验证的网络凭据。  | 
        	
| DefaultCachePolicy | 
		 获取或设置此请求的默认缓存策略。  | 
        	
| DefaultWebProxy | 
		 获取或设置全局 HTTP 代理。  | 
        	
| Headers | 
		 在后代类中重写时,获取或设置与请求关联的标头名称/值对的集合。  | 
        	
| ImpersonationLevel | 
		 获取或设置当前请求的模拟级别。  | 
        	
| Method | 
		 在后代类中重写时,获取或设置在此请求中使用的协议方法。  | 
        	
| PreAuthenticate | 
		 在后代类中重写时,指示是否预先对请求进行身份验证。  | 
        	
| Proxy | 
		 在后代类中重写时,获取或设置用于访问此 Internet 资源的网络代理。  | 
        	
| RequestUri | 
		 在后代类中重写时,获取与请求关联的 Internet 资源的 URI。  | 
        	
| Timeout | 
		 获取或设置请求超时前的时间长度(以毫秒为单位)。  | 
        	
| UseDefaultCredentials | 
		 在后代类中重写时,获取或设置一个 Boolean 值,该值控制是否使用请求发送 DefaultCredentials。  | 
        	
方法
显式接口实现
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) | 
			 
				已过时.
			 
		在后代类中重写时,使用序列化 WebRequest所需的数据填充 SerializationInfo 实例。  |