浏览器如何确定Accept标头?

时间:2014-11-28 20:03:22

标签: html http web

所以我在本地有这个网页:

<html>
<head>
    <title>Sample "Hello, World" Application</title>
</head>
<body bgcolor=white>
<table border="0">
    <tr>
        <td>
            <img src="images/tomcat.gif">
        </td>
        <td>
            <h1>Sample "Hello, World" Application</h1>
            <p>This is the home page for a sample application used to illustrate the
                source directory organization of a web application utilizing the principles
                outlined in the Application Developer's Guide.
        </td>
    </tr>
</table>
<p>To prove that they work, you can execute either of the following links:
<ul>
    <li>To a <a href="hello.jsp">JSP page</a>.
    <li>To a <a href="hello">servlet</a>.
</ul>
</body>
</html>

当我追踪我的HTTP请求和响应时,我会看到:

GET /sample/ HTTP/1.1
Host: localhost:8080
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, lzma, sdch
Accept-Language: tr
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36 OPR/26.0.1656.24

HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 636
Content-Type: text/html
Date: Fri, 28 Nov 2014 19:48:47 GMT
ETag: W/"636-1185801988000"
Last-Modified: Mon, 30 Jul 2007 13:26:28 GMT
Server: Apache-Coyote/1.1

并且第二次请求图像:

GET /sample/images/tomcat.gif HTTP/1.1
Host: localhost:8080
Accept: image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, lzma, sdch
Accept-Language: tr
Referer: http://localhost:8080/sample/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36 OPR/26.0.1656.24

HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 1441
Content-Type: image/gif
Date: Fri, 28 Nov 2014 19:54:55 GMT
ETag: W/"1441-1185801988000"
Last-Modified: Mon, 30 Jul 2007 13:26:28 GMT
Server: Apache-Coyote/1.1

为什么浏览器会发送Accept: image/webp,*/*;q=0.8

当我点击图片本身并在新标签中打开它时,请求将发送为:

GET /sample/images/tomcat.gif HTTP/1.1
Host: localhost:8080
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, lzma, sdch
Accept-Language: tr
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36 OPR/26.0.1656.24

HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 1441
Content-Type: image/gif
Date: Fri, 28 Nov 2014 19:55:53 GMT
ETag: W/"1441-1185801988000"
Last-Modified: Mon, 30 Jul 2007 13:26:28 GMT
Server: Apache-Coyote/1.1

为什么第二个请求在Accept标头中有更多的值,但在第一种情况下它是不同的?

1 个答案:

答案 0 :(得分:3)

它本质上是浏览器制造商希望能够轻松识别支持的格式的历史记录。

格赖斯指出,这些都包括*/*,所以接受任何东西;指定的格式只是首选项。

至少在一个案例中:

Accept: image/webp,*/*;q=0.8

只是促进WebP使用的一个特例。 Modify Accept header to explicitly denote the image formats Chrome supports

  

作为促进采用WebP的一部分,如果Chrome在Accept标头中更明确地指出了它支持的图像格式,那将非常有用。目前它只返回* / *,但这使得服务器很难知道是否可以安全地返回WebP图像代替JPEG

由于Chrome显然也接受PNG,JPEG和GIF,因此将WebP作为特例只是尝试鼓励对首选格式的特定支持。同样,application/xhtml+xml的特殊情况在尝试鼓励更广泛地使用XHTML时非常有意义。