Linux curl命令中 HTTP 302处理


在Linux中使用curl命令时,偶尔会遇到一些URL跳转到新的URL,即HTTP中的3XX(redirection,重定向 )。

$curl -s -I $URL > log

这时在返回的报文中,有Location字段标明了新的URL。

$cat log

HTTP/1.1 302 Found
Expires: -1
Cache-Control: private, max-age=0
Pragma: no-cache
Location: http://182.118.2.11/youku/5571865A52945820CFCE162719/03000811074CDDB614769602996C24421AE8D8-C2A3-1BD9-74AC-F5B071A94E33.mp4
Content-type: text/html
Connection: close
Date: Tue, 05 Apr 2011 08:37:07 GMT
Server: F_LIGHTY_TJ8

当我试图获取这个新的URL,再进行curl时,总是报HTTP 400 (Bad Request,错误的请求)。

$URL=`cat log | grep '^Location' | cut -f2 -d ' ' `
$curl -s -I $URL > log

$cat log

HTTP/1.1 400 Bad Request
Content-Type: text/html
Content-Length: 349
Connection: close
Date: Tue, 05 Apr 2011 08:32:04 GMT
Server: YOUKU.ZZ

直接将新的URL打印出来,复制粘贴再curl,却没有问题 。

$curl -s -I http://182.118.2.11/youku/5571865A52945820CFCE162719/03000811074CDDB614769602996C24421AE8D8-C2A3-1BD9-74AC-F5B071A94E33.mp4

HTTP/1.1 200 OK
Content-Type: video/mp4
Accept-Ranges: bytes
ETag: "392853628"
Last-Modified: Fri, 12 Nov 2010 21:48:23 GMT
Content-Length: 19971197
Connection: close
Date: Tue, 05 Apr 2011 08:41:18 GMT
Server: YOUKU.ZZ

百思不得其解!

最后。发现这个新的$URL中和“http://182.118.2.11/youku/5571865A52945820CFCE162719/03000811074CDDB614769602996C24421AE8D8-C2A3-1BD9-74AC-F5B071A94E33.mp4”并不完全相同,$URl的结尾多了一个‘0A’换行符。

$echo $URL > url_1

$echo http://182.118.2.11/youku/5571865A52945820CFCE162719/03000811074CDDB614769602996C24421AE8D8-C2A3-1BD9-74AC-F5B071A94E33.mp4 > url_2

$hexdump url_1

0000000 7468 7074 2f3a 312f 3238 312e 3831 322e
0000010 312e 2f31 6f79 6b75 2f75 3935 3337 4538
0000020 3244 4637 3335 3834 3831 4239 4144 4336
0000030 4533 3941 302f 3033 3030 3138 3031 3437
0000040 4443 4244 3136 3734 3936 3036 3932 3639
0000050 3243 3434 3132 4541 4438 2d38 3243 3341
0000060 312d 4442 2d39 3437 4341 462d 4235 3730
0000070 4131 3439 3345 2e33 706d 0d34 000a

$hexdump url_2

0000000 7468 7074 2f3a 312f 3238 312e 3831 322e
0000010 312e 2f31 6f79 6b75 2f75 3935 3337 4538
0000020 3244 4637 3335 3834 3831 4239 4144 4336
0000030 4533 3941 302f 3033 3030 3138 3031 3437
0000040 4443 4244 3136 3734 3936 3036 3932 3639
0000050 3243 3434 3132 4541 4438 2d38 3243 3341
0000060 312d 4442 2d39 3437 4341 462d 4235 3730
0000070 4131 3439 3345 2e33 706d 0a34

去掉这个换行符,一切OK

URL=`cat log | grep '^Location' | cut -f2 -d ' ' |sed 's/\r//' `

Been dazed and confused for so long

相关内容