Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WebResponse.ContentType Property

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

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

public virtual string ContentType { get; set; }
/** @property */
public String get_ContentType ()

/** @property */
public void set_ContentType (String value)

public function get ContentType () : String

public function set ContentType (value : String)

Not applicable.

Property Value

A string that contains the content type of the response.
Exception typeCondition

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 MIME content type of the response from the Internet resource, if known.

NoteNote:

The WebResponse class is an abstract 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.

The following example uses the ContentType property to obtain the content type of the response.


         // 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(); 


// 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}", 
    System.Convert.ToString(myWebResponse.get_ContentLength()),
    System.Convert.ToString(myWebResponse.get_ContentType()));
// Release resources of response object.
myWebResponse.Close();

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.