WebRequest.Headers 属性

定义

在后代类中重写时,获取或设置与请求关联的标头名称/值对的集合。

public:
 abstract property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public:
 virtual property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public abstract System.Net.WebHeaderCollection Headers { get; set; }
public virtual System.Net.WebHeaderCollection Headers { get; set; }
member this.Headers : System.Net.WebHeaderCollection with get, set
Public MustOverride Property Headers As WebHeaderCollection
Public Overridable Property Headers As WebHeaderCollection

属性值

包含与此请求关联的标头名称/值对的 WebHeaderCollection

例外

如果未在子代类中重写属性,则会尝试获取或设置该属性。

示例

以下示例显示与此请求关联的标头名称/值对。

// Create a new request to the mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( "http://www.contoso.com" );

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

// Release the resources of response object.
myWebResponse->Close();
Console::WriteLine( "\nThe HttpHeaders are \n {0}", myWebRequest->Headers );

         // Create a new request to the mentioned URL.	
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");		

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();

// Release the resources of response object.
myWebResponse.Close();
Console.WriteLine("\nThe HttpHeaders are \n{0}",myWebRequest.Headers);

    ' Create a new request to the mentioned URL.	
    Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")
    
   ' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
   
    Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
   ' Release the resources of response object.
   
 myWebResponse.Close()
Console.WriteLine(ControlChars.Cr + "The HttpHeaders are " + ControlChars.Cr + "{0}", myWebRequest.Headers)

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

Headers 属性包含一个 WebHeaderCollection 实例,该实例包含要发送到 Internet 资源的标头信息。

注意

WebRequest 类是 abstract 类。 运行时 WebRequest 实例的实际行为由 WebRequest.Create 方法返回的后代类确定。 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequestFileWebRequest

适用于

另请参阅