Was this page helpful?
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HttpWebResponse.ContentType Property


Gets the content type of the response.

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

public override string ContentType { get; }

Property Value

Type: System.String

A string that contains the content type of the response.

Exception Condition

The current instance has been disposed.

The ContentType property contains the value of the Content-Type header returned with the response.

The following example displays the value of this property.

using System;
using System.Net;
using System.Text;
using System.IO;

    public class Test
        // Specify the URL to receive the request.
        public static void Main (string[] args)
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

            Console.WriteLine ("Content length is {0}", response.ContentLength);
            Console.WriteLine ("Content type is {0}", response.ContentType);

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream ();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

            Console.WriteLine ("Response stream received.");
            Console.WriteLine (readStream.ReadToEnd ());
            response.Close ();
            readStream.Close ();

The output from this example will vary depending on the value passed into Main 
but will be similar to the following:

Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.


Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft