Linux curl命令中 HTTP 302處理
Linux系統(tǒng)中使用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/557186***52945820CFCE162719/03000811074CDDB614769602996C24421AE8D8-C2A3-1BD9-74AC-F5B071A94E33.MP4 Content-type: text/html Connection: close Date: Tue, 0***pr 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, 0***pr 2011 08:32:04 GMT Server: YOUKU.ZZ
直接將新的URL打印出來,復制粘貼再curl,卻沒有問題 。
$curl -s -I http://182.118.2.11/youku/557186***52945820CFCE162719/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, 0***pr 2011 08:41:18 GMT Server: YOUKU.ZZ
百思不得其解!
***。發(fā)現(xiàn)這個新的$URL中和“http://182.118.2.11/youku/557186***52945820CFCE162719/03000811074CDDB614769602996C24421AE8D8-C2A3-1BD9-74AC-F5B071A94E33.MP4”并不完全相同,$URl的結尾多了一個‘0A’換行符。
$echo $URL > url_1 $echo http://182.118.2.11/youku/557186***52945820CFCE162719/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
【編輯推薦】
- Linux文件分割與合并:split&cat
- 聚焦Linux合作峰會2011:五大議題
- Linux進程與作業(yè)的區(qū)別及相關命令
- Linux下利用ext3grep恢復被刪除的文件
- Novell交易還未完成 SUSE Linux何去何從?