Export (0) Print
Expand All

WebResponse.ContentType Property

When overridden in a derived class, gets or sets the content type of the data being received.

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

Property Value

A string containing the content type of the response.

Exceptions

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

Remarks

The ContentType property contains the MIME content type of the response from the Internet resource, if known.

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

Example

[Visual Basic, C#, C++] The following example uses the ContentType property to obtain the content type of the response.

[Visual Basic] 

' Create a 'WebRequest' with the specified url.     
Dim myWebRequest As WebRequest = WebRequest.Create("www.contoso.com")

' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

' The ContentLength and ContentType received as headers in the response object are also exposed as properties.
   ' These provide information about the length and type of the entity body in the response.
Console.WriteLine(ControlChars.Cr + "Content length :{0}, Content Type : {1}", myWebResponse.ContentLength, myWebResponse.ContentType)
myWebResponse.Close()


[C#] 

                     // Create a 'WebRequest' with the specified url.
            WebRequest myWebRequest = WebRequest.Create("http://www.contoso.com"); 

            // Send the 'WebRequest' and wait for response.
            WebResponse myWebResponse = myWebRequest.GetResponse(); 

            // Display the content length and content type received as headers in the response object.
            Console.WriteLine("\nContent length :{0}, Content Type : {1}", 
                                         myWebResponse.ContentLength, 
                                         myWebResponse.ContentType);  
            
            // Release resources of response object.
            myWebResponse.Close(); 
            

[C++] 
// Create a 'WebRequest' with the specified url.
WebRequest* myWebRequest = WebRequest::Create(S"http://www.contoso.com");

// Send the 'WebRequest' and wait for response.
WebResponse* myWebResponse = myWebRequest->GetResponse();

// Display the content length and content type received as headers in the response object.
Console::WriteLine(S"\nContent length : {0}, Content Type : {1}", __box(myWebResponse->ContentLength),
   myWebResponse->ContentType);

// Release resources of response object.
myWebResponse->Close();

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

Requirements

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

WebResponse Class | WebResponse Members | System.Net Namespace | HttpWebResponse.ContentType

Show:
© 2014 Microsoft