响应中的HTTP标头包含CURL请求,但不包含浏览器请求 [英] HTTP Header in Response exists with a CURL request but not with Browser request

查看:105
本文介绍了响应中的HTTP标头包含CURL请求,但不包含浏览器请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在对象上运行cURL以查看其HTTP响应标头时(我刚刚添加了代理标志来模拟浏览器请求):

When I run cURL on an object to see its HTTP response headers (I just added agent flag to emulate a browser request):

curl -I -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36" http://b17db03a092ebf708b42-9afd7fe2d9c9c6937aeab44904ac3403.r3.cf5.rackcdn.com/artist/samplesong/13111c20aee51aeb480ecbd988cd8cc9.mp3

我有:


HTTP/1.1 200 OK
Content-Length: 481005
Accept-Ranges: bytes
Last-Modified: Tue, 08 Sep 2015 18:37:54 GMT
ETag: e9f52648c48c707cb9c17e1871787ad8
X-Timestamp: 1441737473.83703
Access-Control-Allow-Origin: *
Content-Type: audio/mpeg
X-Trans-Id: tx6531dbbd8dc541eeaf824-0055f0765eiad3
Cache-Control: public, max-age=259169
Expires: Sat, 12 Sep 2015 18:11:11 GMT
Date: Wed, 09 Sep 2015 18:11:42 GMT
Connection: keep-alive

A s可以看到,有一个
访问控制允许来源行。

As you can see, there is a Access-Control-Allow-Origin line.

但是当我要求完全相同时带有Safari,Chrome或Firefox(无缓存)的URL:

But when I request the exact same URL with Safari, Chrome or Firefox (without cache):


Accept-Ranges:bytes
Cache-Control:public, max-age=55773
Connection:keep-alive
Content-Length:1
Content-Range:bytes 203184-203184/480621
Content-Type:audio/mpeg
Date:Wed, 09 Sep 2015 18:14:12 GMT
ETag:b1d4c80b2d5d42a4bf6c1ae5efb289e6
Expires:Thu, 10 Sep 2015 09:43:45 GMT
Last-Modified:Mon, 07 Sep 2015 09:19:19 GMT
X-Timestamp:1441617558.54283
X-Trans-Id:txe8e3941ab1774f4d97794-0055ed5c71iad3

Access-Control-Allow-Origin 消失了!

有人知道为什么http响应标头可与cURL一起使用,但不能与Browser一起使用吗?谢谢。

Any idea why the http response header works with cURL but not with Browser ? Thanks.

推荐答案

cURL和您的浏览器行为之间没有区别。

There is no difference between cURL and your browser behaviour.

在curl调用中添加-I标志时,将发送 HEAD请求。在您的浏览器中,这是一个 GET请求。

When you add the -I flag in your curl call, you send a 'HEAD' request. In your browser, it was a 'GET' request.

如果使用-i标志(-i在输出中包含HTTP标头)来卷曲此URL,则将丢失 Access-Control-Allow-原始行:

If you curl this url with the -i flag (-i includes the HTTP-header in the output.), you will lose the "Access-Control-Allow-Origin" line :

curl -i -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36" http://b17db03a092ebf708b42-9afd7fe2d9c9c6937aeab44904ac3403.r3.cf5.rackcdn.com/artist/samplesong/13111c20aee51aeb480ecbd988cd8cc9.mp3 > C:/curl_return.log

如果打开文件,您会得到:

And if you open the file you got :

HTTP/1.1 200 OK
Content-Length: 480621
Accept-Ranges: bytes
Last-Modified: Mon, 07 Sep 2015 09:19:19 GMT
ETag: b1d4c80b2d5d42a4bf6c1ae5efb289e6
X-Timestamp: 1441617558.54283
Content-Type: audio/mpeg
X-Trans-Id: tx76217fcbee85481d844ab-0055ed6d83iad3
Cache-Control: public, max-age=56913
Expires: Thu, 10 Sep 2015 10:57:04 GMT
Date: Wed, 09 Sep 2015 19:08:31 GMT
Connection: keep-alive

ID3.................................................................
....................................................................

这篇关于响应中的HTTP标头包含CURL请求,但不包含浏览器请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆