Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get content type of a web address?

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?

like image 332
ahmadali shafiee Avatar asked Aug 14 '12 02:08

ahmadali shafiee


3 Answers

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.

like image 86
EdFred Avatar answered Sep 29 '22 20:09

EdFred


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;
    }
like image 41
Dmitry Khryukin Avatar answered Sep 29 '22 22:09

Dmitry Khryukin


HTTP Response header: content-type

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

like image 27
Immersive Avatar answered Sep 29 '22 20:09

Immersive