How to get content type of a web address?

前端 未结 5 393
故里飘歌
故里飘歌 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 22:56

    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.

提交回复
热议问题