Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WebClient.Headers Property

Gets or sets a collection of header name/value pairs associated with the request.

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

Property Value

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

Remarks

The Headers property contains a WebHeaderCollection instance containing header informationthat the WebClient sends to the Internet resource. This is an unrestricted collection of headers, so setting headers that are restricted by WebRequest descendants such as HttpWebRequest is allowed.

Example

[Visual Basic, C#, C++] The following example uses the Headers collection to set the HTTP Content-Type header to "application/x-www-form-urlencoded" to notify the server that form data is attached to the post.

[Visual Basic] 
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, http://www.contoso.com} : ")
uriString = Console.ReadLine()
' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
' Upload the input string using the HTTP 1.0 POST method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, "POST", byteArray)
' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{0}", Encoding.ASCII.GetString(responseArray))

[C#] 
string uriString;
Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
uriString = Console.ReadLine();
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
string postData = Console.ReadLine();
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
// Apply ASCII Encoding to obtain the string as a byte array.
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
Console.WriteLine("Uploading to {0} ...",  uriString);                        
// Upload the input string using the HTTP 1.0 POST method.
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
// Decode and display the response.
Console.WriteLine("\nResponse received was {0}",
    Encoding.ASCII.GetString(responseArray));

[C++] 
String* uriString;
Console::Write(S"\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
uriString = Console::ReadLine();
// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();
Console::WriteLine(S"\nPlease enter the data to be posted to the URI {0}:", uriString);
String* postData = Console::ReadLine();
myWebClient->Headers->Add(S"Content-Type", S"application/x-www-form-urlencoded");
// Apply ASCII Encoding to obtain the String* as a Byte array.
Byte byteArray[] = Encoding::ASCII->GetBytes(postData);
Console::WriteLine(S"Uploading to {0} ...",  uriString);
// Upload the input String* using the HTTP 1.0 POST method.
Byte responseArray[] = myWebClient->UploadData(uriString, S"POST", byteArray);
// Decode and display the response.
Console::WriteLine(S"\nResponse received was {0}",
   Encoding::ASCII->GetString(responseArray));

[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, Common Language Infrastructure (CLI) Standard

See Also

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

Show:
© 2014 Microsoft. All rights reserved.