WebClient.Headers Property

Definition

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

C#
public System.Net.WebHeaderCollection Headers { get; set; }

Property Value

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

Examples

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.

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

 // Display the headers in the request
       Console.Write("Resulting Request Headers: ");
       Console.WriteLine(myWebClient.Headers.ToString());
       
       // 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));
                 

Remarks

Výstraha

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

The Headers property contains a WebHeaderCollection instance containing protocol headers that the WebClient sends with the request.

Some common headers are considered restricted and are protected by the system and cannot be set or changed in a WebHeaderCollection object. Any attempt to set one of these restricted headers in the WebHeaderCollection object associated with a WebClient object will throw an exception later when attempting to send the WebClient request.

Restricted headers protected by the system include, but are not limited to the following:

  • Date

  • Host

In addition, some other headers are also restricted when using a WebClient object. These restricted headers include, but are not limited to the following:

  • Accept

  • Connection

  • Content-Length

  • Expect (when the value is set to "100-continue"

  • If-Modified-Since

  • Range

  • Transfer-Encoding

The HttpWebRequest class has properties for setting some of the above headers. If it is important for an application to set these headers, then the HttpWebRequest class should be used instead of the WebRequest class.

You should not assume that the header values will remain unchanged, because Web servers and caches may change or add headers to a Web request.

Applies to

Produkt Verzie
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also