C# WebClient Headers

Description

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

Syntax

WebClient.Headers has the following syntax.


public WebHeaderCollection Headers { get; set; }

Example

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.


/*from   w ww .j a  va  2 s. c o  m*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class Example
{
    public static void Main()
   {
            string uriString = "http://www.java2s.com";
      WebClient myWebClient = new WebClient();
      string postData = "data";
      myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

      Console.WriteLine(myWebClient.Headers.ToString());
      
      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);
      
          Encoding.ASCII.GetString(responseArray);
    
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Net »




Cookie
Dns
IPAddress
IPEndPoint
IPHostEntry
WebClient