简体   繁体   English

如何获取网址的内容类型?

[英]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 . 例如是一个HTML页面,其页面类型为text/html ,但类型text/xml this page's type seems to be image/png but it's text/html . 这个页面的类型似乎是image/png但它是text/html

I want to know how can I detect the content type of a web address like this ? 我想知道我怎么可以检测如Web地址的内容类型

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;
    }

HTTP Response header: content-type HTTP响应标头: content-type

For a more detailed response, please provide a more detailed question. 如需更详细的回复,请提供更详细的问题。

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 您可以通过响应的Http标头检测Content-Type ,对于http://bayanbox.ir/user/ahmadalli/images/div.png ,标题是

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

Read up on HTTP headers. 阅读HTTP标头。

HTTP headers will tell you the content type. HTTP标头将告诉您内容类型。 For example: 例如:

content-type: application/xml. content-type:application / xml。

There are two ways to determining the content-type 有两种方法可以确定内容类型

  1. the file extension invoked by the URL URL调用的文件扩展名
  2. the http header content-type http标头内容类型

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 然后,如果服务器可以提供其中一个并选择XML,它将返回带有标头的内容

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. 而不是使用自己的标头进行字符编码。

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. 无需下载页面即可从标题中获取mime类型。 Just look for the content-type in the response headers. 只需在响应标头中查找内容类型即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM