Export (0) Print
Expand All

WebRequest.Headers Property

When overridden in a descendant class, gets or sets the collection of header name/value pairs associated with the request.

[Visual Basic]
Public Overridable Property Headers As WebHeaderCollection
[C#]
public virtual WebHeaderCollection Headers {get; set;}
[C++]
public: __property virtual WebHeaderCollection* get_Headers();
public: __property virtual void set_Headers(WebHeaderCollection*);
[JScript]
public function get Headers() : WebHeaderCollection;
public function set Headers(WebHeaderCollection);

Property Value

A WebHeaderCollection containing the header name/value pairs associated with this request.

Exceptions

Exception Type Condition
NotSupportedException Any attempt is made to get or set the property, when the property is not overridden in a descendant class.

Remarks

The Headers property contains a WebHeaderCollection instance containing the header information to send to the Internet resource.

Note   The WebRequest class is an abstract (MustInherit in Visual Basic) class. The actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

Example

[Visual Basic, C#, C++] The following example displays the header name/value pairs associated with this request.

[Visual Basic] 

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


[C#] 

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

[C++] 
// Create a new request to the mentioned URL.
WebRequest* myWebRequest=WebRequest::Create(S"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(S"\nThe HttpHeaders are \n {0}", myWebRequest->Headers);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

WebRequest Class | WebRequest Members | System.Net Namespace | WebHeaderCollection | HttpWebRequest.Headers

Show:
© 2014 Microsoft