HttpWebRequest.ContentType Property

May 11, 2014

Gets or sets the value of the Content-type HTTP header.

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

public override string ContentType { get; set; }

Property Value

Type: System.String
The value of the Content-type HTTP header. The default value is null.

The ContentType property contains the media type of the request. Values assigned to the ContentType property replace any existing contents when the request sends the Content-type HTTP header.

To clear the Content-type HTTP header, set the ContentType property to null.

NoteNote:

The value for this property is stored in a WebHeaderCollection object. If WebHeaderCollection is set, the property value for ContentType is lost.


        // Change this Uri to a public server
        System.Uri myUri = new Uri("http://www.contoso.com");

        // Create a 'HttpWebRequest' object.
        HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(myUri);

        // Set the 'Method' property to "POST".
        myHttpWebRequest.Method="POST";

        // Set the 'ContentType' property.
        myHttpWebRequest.ContentType="application/x-www-form-urlencoded";

        // Get the 'ContentType' property and print the current value
        outputBlock.Text += "HttpWebRequest.ContentType: ";
        outputBlock.Text += myHttpWebRequest.ContentType;
        outputBlock.Text += "\n";



Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft