【问题标题】:How does browser determine the Accept header?浏览器如何确定 Accept 标头?
【发布时间】:2015-01-27 12:02:12
【问题描述】:

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

<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 标头中有更多的值,但在第一种情况下却不同?

【问题讨论】:

  • 从技术上讲,它们都接受相同的东西,因为*/* 接受一切(任何媒体/任何子类型)。
  • 含义:接受:text/html, application/xhtml+xml, image/jxr, /

标签: html http web


【解决方案1】:

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

正如 Grice 所指出的,这些都包括*/*,所以接受任何东西;指定的格式只是偏好。

至少在一种情况下:

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 时也很有意义。

【讨论】:

  • 含义:接受:text/html, application/xhtml+xml, image/jxr, / , no stars
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
相关资源
最近更新 更多