Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

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.
ExceptionCondition
ObjectDisposedException

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.
<html>
...
</html>

*/
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;

// Specify the URL to receive the request.
int main ()
{
    String* args[] = Environment::GetCommandLineArgs();

    HttpWebRequest* request = dynamic_cast<HttpWebRequest*>(WebRequest::Create (args[1]));

    // 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 = dynamic_cast<HttpWebResponse*>(request->GetResponse ());

    Console::WriteLine (S"Content length is {0}", __box(response->ContentLength));
    Console::WriteLine (S"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 (S"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.
<html>
...
</html>

*/

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 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.