How to get content type of web address?

I want to get the type of web address. For example, this is an Html page and its page type is text/html , but the type is text/xml . this type of page is of type image/png , but text/html .

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

+6
content-type c #
source share
5 answers

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; } 
+9
source share

HTTP response header: content-type

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

0
source share

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

 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 
0
source share
 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); } } 

You will get the mime type from the headers without loading the page. Just find the content type in the response headers.

0
source share

Reading HTTP headers.

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

content-type: application / xml.

There are two ways to determine the type of content.

  • file extension called by url
  • http header content type

The first was somewhat advanced by Microsoft in the old days and is no longer good practice.

If the client has display restrictions that accept only a certain type of content, it requests a server with headers, for example

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

And then, if the server can provide one of them and select XML, it will return the contents with the header

 content-type: application/xml. 

However, some services include additional information, such as

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

rather than using your own caption to encode characters.

0
source share

All Articles