How to get content type of a web address?

13,008

Solution 1

it should be something like this

    var request = HttpWebRequest.Create("http://www.google.com") as HttpWebRequest;
    if (request != null)
    {
        var response = request.GetResponse() as HttpWebResponse;

        string contentType = "";

        if (response != null)
            contentType = response.ContentType;
    }

Solution 2

using (MyClient client = new MyClient())
    {
        client.HeadOnly = true;
        string uri = "http://www.google.com";
        byte[] body = client.DownloadData(uri); // note should be 0-length
        string type = client.ResponseHeaders["content-type"];
        client.HeadOnly = false;
        // check 'tis not binary... we'll use text/, but could
        // check for text/html
        if (type.StartsWith(@"text/"))
        {
            string text = client.DownloadString(uri);
            Console.WriteLine(text);
        }
    }

Will get you the mime type from the headers without downloading the page. Just look for the content-type in the response headers.

Solution 3

You can detect the Content-Type by the Http header of the response,for http://bayanbox.ir/user/ahmadalli/images/div.png ,the header is

Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Tue, 14 Aug 2012 03:01:41 GMT
Server:bws
Transfer-Encoding:chunked
Vary:Accept-Encoding

Solution 4

HTTP Response header: content-type

For a more detailed response, please provide a more detailed question.

Solution 5

Read up on HTTP headers.

HTTP headers will tell you the content type. For example:

content-type: application/xml.

There are two ways to determining the content-type

  1. the file extension invoked by the URL
  2. the http header content-type

The first one was somewhat promoted by microsoft during to old days and is not a good practice anymore.

If the client has display constraints accepting only certain content-type, it would request the server with the headers like

accept: application/json
accept: text/html
accept: application/xml

And then if the server could supply one of those and chooses XML it would return the content with the header

content-type: application/xml.

However, some services include further information like

content-type: application/xml; charset=utf-8

rather than using a header of its own for the character encoding.

Share:
13,008

Related videos on Youtube

ahmadali shafiee
Author by

ahmadali shafiee

I Love Programming A Lot!!! but now it is not correct! computer is my life. I do every thing using computer. but programming is the must used tool in computer. Started with Pascal and now is C#.

Updated on September 15, 2022

Comments

  • ahmadali shafiee
    ahmadali shafiee over 1 year

    I want to get type of a web address. For example this is a Html page and its page type is text/html but the type of this is text/xml. this page's type seems to be image/png but it's text/html.

    I want to know how can I detect the content type of a web address like this?

  • dotancohen
    dotancohen over 11 years
    OP asked how to get that information in C#, not what the header is.
  • dotancohen
    dotancohen over 11 years
    OP asked how to get that information in C#, not what the header is.
  • Marc Gravell
    Marc Gravell over 11 years
    Presumably MyClient is a subclass of WebClient with HEAD support?
  • EdFred
    EdFred over 11 years
    Yes you are correct. This was copied from another sample I worked on for checking for binary http responses.
  • Marc Gravell
    Marc Gravell over 11 years
    it may be mre useful to readers if you link to that other sample :)
  • ahmadali shafiee
    ahmadali shafiee over 11 years
    I also used your code here thanks.
  • Anand Sainath
    Anand Sainath over 8 years
    WebClient with method support -
  • IS4
    IS4 over 7 years
    request.Method = "HEAD"; may also be added for speed.
  • mekb
    mekb almost 5 years
    if (response != null) contentType = response.ContentType; can be shorted to contentType = response?.ContentType; (info here)