Export (0) Print
Expand All

WebClient.Headers Property

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

Namespace: System.Net
Assembly: System (in system.dll)

public WebHeaderCollection Headers { get; set; }
/** @property */
public WebHeaderCollection get_Headers ()

/** @property */
public void set_Headers (WebHeaderCollection value)

public function get Headers () : WebHeaderCollection

public function set Headers (value : WebHeaderCollection)

Not applicable.

Property Value

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

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

The following code 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.

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

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.get_Headers().Add("Content-Type",
    "application/x-www-form-urlencoded");
// Apply ASCII Encoding to obtain the string as a byte array.
ubyte byteArray[] = Encoding.get_ASCII().GetBytes(postData);
Console.WriteLine("Uploading to {0} ...", uriString);
// Upload the input string using the HTTP 1.0 POST method.
ubyte responseArray[] = myWebClient.UploadData(uriString, "POST",
    byteArray);
// Decode and display the response.
Console.WriteLine("\nResponse received was {0}", Encoding.
    get_ASCII().GetString(responseArray));

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft