根据我的理解:
HTTP clients
使用Accept标头告诉服务器他们接受哪些内容类型。然后,服务器将发回一个响应,该响应将包含一个Content-Type标头,告诉客户端返回内容的内容类型实际上是什么。
基于这种理解,我尝试了以下方法:
curl -X GET -H "Accept: application/xml" http://www.google.com -v
* About to connect() to www.google.com port 80 (#0)
* Trying 173.194.33.81...
* connected
* Connected to www.google.com (173.194.33.81) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5
> Host: www.google.com
> Accept: application/xml
>
< HTTP/1.1 200 OK
< Date: Tue, 02 Sep 2014 17:58:05 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< Set-Cookie: PREF=ID=5c30672b67a74789:FF=0:TM=1409680685:LM=1409680685:S=PsGclk3vR4HWjann; expires=Thu, 01-Sep-2016 17:58:05 GMT; path=/; domain=.google.com
< Set-Cookie: NID=67=rPuxpwUu5UNuapzCdbD5iwVyjjC9TzP_Ado29h3ucjEq4A_2qkSM4nQM3RO02rfyuHmrh-hvmwmgFCmOvISttFfHv06f8ay4_6Gl4pXRjqxihNhJSGbvujjDRzaSibfy; expires=Wed, 04-Mar-2015 17:58:05 GMT; path=/; domain=.google.com; HttpOnly
< P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Alternate-Protocol: 80:quic
< Transfer-Encoding: chunked
<
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><
正如你在回复中所注意到的那样,我被发送Content-Type: text/html; charset=ISO-8859-1
这不是我要求的?
为什么会发送不同的表示形式(在这种情况下为HTML
),尽管我要求xml
?
由于
答案 0 :(得分:1)
来自RFC 2616:
如果存在Accept标头字段, 如果服务器无法发送可接受的响应 根据组合的Accept字段值,那么服务器应该 发送406(不接受)回复。
在这里,&#34;应该&#34;表示Google实际上没有强制抛出406错误。但是,由于您正在接收HTML响应,因此它具有相同的含义。