Linux系統中使用curl命令時,偶爾會遇到一些URL跳轉到新的URL,即HTTP中的3XX(redirection,重定向,如HTTP 302 )。
$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 322e0000010 312e 2f31 6f79 6b75 2f75 3935 3337 45380000020 3244 4637 3335 3834 3831 4239 4144 43360000030 4533 3941 302f 3033 3030 3138 3031 34370000040 4443 4244 3136 3734 3936 3036 3932 36390000050 3243 3434 3132 4541 4438 2d38 3243 33410000060 312d 4442 2d39 3437 4341 462d 4235 37300000070 4131 3439 3345 2e33 706d 0d34 000a
$hexdump url_20000000 7468 7074 2f3a 312f 3238 312e 3831 322e0000010 312e 2f31 6f79 6b75 2f75 3935 3337 45380000020 3244 4637 3335 3834 3831 4239 4144 43360000030 4533 3941 302f 3033 3030 3138 3031 34370000040 4443 4244 3136 3734 3936 3036 3932 36390000050 3243 3434 3132 4541 4438 2d38 3243 33410000060 312d 4442 2d39 3437 4341 462d 4235 37300000070 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