Content negotiation refers to mechanisms defined as a part of HTTP that make it possible to serve different versions of a document (or more generally, representations of a resource) at the same URI, so that user agents can specify which version fits their capabilities the best. One classical use of this mechanism is to serve an image in GIF or PNG format, so that a browser that cannot display PNG images (e.g. MS Internet Explorer 4) will be served the GIF version.
内容协商是HTTP的机制,该机制使得同一URI上可以提供文档的不同版本(或更笼统地说,资源的表示形式),以便user agents可以指定最适合的版本。这种机制的一种经典用法是提供GIF或PNG格式的图像,以便将无法显示PNG图像的浏览器(例如MS Internet Explorer 4)提供给GIF版本。
A resource may be available in several different representations; for example, it might be available in different languages or different media types. One way of selecting the most appropriate choice is to give the user an index page and let them select the most appropriate choice; however it is often possible to automate the choice based on some selection criteria.
资源可能有几种不同的表示形式。例如,它可能以不同的语言或不同的媒体类型提供。选择最合适的选择的一种方法是给用户一个索引页,并让他们选择最合适的选择。但是,通常有可能根据某些选择标准来自动进行选择。
概括:浏览器把偏爱通过header告诉服务器,服务器在内容变体列表中选择合适的形式返回
还有Vary用于协商缓存。
Server-driven or proactive content negotiation is performed by algorithms on the server which choose among the possible variant representations. This is commonly performed based on user agent-provided acceptance criteria.
服务器驱动或主动内容协商由服务器上的算法执行,这些算法在可能的变体表示形式中进行选择。通常根据用户代理提供的接受标准执行此操作。
To summarize how this works, when a user agent submits a request to a server, the user agent informs the server what media types or other aspects of content presentation it understands with ratings of how well it understands them. More precisely, the user agent provides HTTP headers that lists acceptable aspects of the resource and quality factors for them. The server is then able to supply the version of the resource that best fits the user agent’s needs.
概括地说,这是如何工作的,当user agent向服务器提交请求时,user agent会告知服务器它所理解的内容呈现的媒体类型或其他方面,以及对它们的理解程度。更准确地说,user agent提供HTTP headers,其中列出了可接受的资源方面和质量因数。然后,服务器便能够提供最适合user agent需求的资源版本。
For example, a browser could indicate that it would like information in German by setting the Accept-Language like this:
例如,浏览器可以通过这样设置来表明它希望用德语显示信息Accept-Language:
Accept-Language: de
Note that this preference will only be applied when there is a choice of representations and they vary by language.
请注意,只有在可以选择表示形式并且它们随语言而变化时,才会应用此header。(也就是说,若服务端只有1种语言、格式等,则客户端再指定没有意义)
As an example of a more complex request, this browser has been configured to accept French and English, but prefer French, and to accept various media types, preferring HTML over plain text or other text types, and preferring GIF or JPEG over other media types, but also allowing any other media type as a last resort:
作为更复杂的请求的示例,此浏览器已配置为接受法语和英语,但更喜欢法语;接受各种媒体类型,比如文本类型,更偏爱HTML,image类型更偏爱GIF或JPEG ,但也允许任何其他媒体类型作为最后手段:
Accept-Language: fr; q=1.0, en; q=0.5
Accept: text/html; q=1.0, text/*; q=0.8, image/gif; q=0.6, image/jpeg; q=0.6, image/*; q=0.5, */*; q=0.1
与Server-driven相反,服务器将变体列表返回,agent选择。
客户端能够处理的媒体类型,及媒体类型优先级
媒体类型:文本文件text/html、图片文件image/jpeg、视频文件video/mpeg、应用程序使用的二进制文件application/zip
Accept:text/plain;q=0.3,text/htm
权重q范围0~1。默认为1,当服务器提供多种内容时,会优先返回权重值最高的媒体类型。
请求的URL是从哪个web页面发起的
常用于网站的访问统计:比如我在很多地方都做了广告链接到我网站的主页,这时候我就可以通过referer来查看哪些地方跳转过来的人多,说明广告效果好;
也可以用于防盗链
The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.
以Firefox为例:
Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0
Mozilla/5.0 是一个通用标记符号,用来表示与 Mozilla 兼容,这几乎是现代浏览器的标配。
platform:浏览器锁运行的原生系统平台(Windows、Mac、Linux、Android)
rv:geckoversion:Gecko发布的版本号。Gecko是Rendering Engine,也是浏览器的内核。(常见的浏览器内核参考https://www.cnblogs.com/baihuitestsoftware/articles/6689012.html)
Gecko/geckotrail:表示该浏览器基于Gecko渲染引擎,在桌面浏览器中,geckotrail 是固定字符串20100101
Firefox/firefoxversion:表示该浏览器是Firefox,版本信息为75.0
其他浏览器User-Agent参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
具体见:https://blog.csdn.net/jiangshangchunjiezi/article/details/88581824
响应报文
具体见:https://blog.csdn.net/jiangshangchunjiezi/article/details/88591341
具体见:https://blog.csdn.net/jiangshangchunjiezi/article/details/88591818