How to get content type of a web address?

前端 未结 5 383
故里飘歌
故里飘歌 2020-12-03 22:21

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

5条回答
  •  失恋的感觉
    2020-12-03 23:01

    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.

提交回复
热议问题