WebRequest.Headers 属性

定义

当在子类中被重写时,获取或设置与请求关联的标头名称/值对的集合。When overridden in a descendant class, gets or sets the collection of header name/value pairs associated with the request.

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

包含与此请求关联的标头名称/值对的 WebHeaderCollectionA WebHeaderCollection containing the header name/value pairs associated with this request.

例外

当未在子类中重写该属性时,试图获取或设置该属性。Any attempt is made to get or set the property, when the property is not overridden in a descendant class.

示例

下面的示例显示与此请求关联的标头名称/值对。The following example displays the header name/value pairs associated with this request.

// 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)

注解

Headers属性包含一个 WebHeaderCollection 实例,其中包含要发送到 Internet 资源的标头信息。The Headers property contains a WebHeaderCollection instance containing the header information to send to the Internet resource.

备注

WebRequest类是一个 abstract 类。The WebRequest class is an abstract class. WebRequest实例在运行时的实际行为由方法返回的子代类确定 WebRequest.CreateThe actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequestFileWebRequestFor more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

适用于

另请参阅