Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

WebRequest.ContentType Property

When overridden in a descendant class, gets or sets the content type of the request data being sent.

[Visual Basic]
Public Overridable Property ContentType As String
public virtual string ContentType {get; set;}
public: __property virtual String* get_ContentType();
public: __property virtual void set_ContentType(String*);
public function get ContentType() : String;
public function set ContentType(String);

Property Value

The content type of the request data.


Exception Type Condition
NotSupportedException Any attempt is made to get or set the property, when the property is not overridden in a descendant class.


The ContentType property contains the media type of the request. This is typically the MIME encoding of the content.

Note   The WebRequest class is an abstract (MustInherit in Visual Basic) class. The actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.


[Visual Basic, C#, C++] The following example sets the ContentType property to the appropriate media type.

[Visual Basic] 

' Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType = "application/x-www-form-urlencoded"

' Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength = byteArray.Length
Dim newStream As Stream = myWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)

' Close the Stream object.

' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()


            // Set the 'ContentType' property of the WebRequest.

            // Set the 'ContentLength' property of the WebRequest.
            Stream newStream=myWebRequest.GetRequestStream();

            // Close the Stream object.

            // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
            WebResponse myWebResponse=myWebRequest.GetResponse();

// Set the 'ContentType' property of the WebRequest.

// Set the 'ContentLength' property of the WebRequest.
Stream* newStream=myWebRequest->GetRequestStream();
newStream->Write(byteArray, 0, byteArray->Length);

// Close the Stream Object*.

// Assign the response Object* of 'WebRequest' to a 'WebResponse' variable.
WebResponse* myWebResponse=myWebRequest->GetResponse();

[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.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

WebRequest Class | WebRequest Members | System.Net Namespace | HttpWebRequest.ContentType

© 2016 Microsoft