Linux curl命令中 HTTP 302處理

來源:互聯網
上載者:User

Linux系統中使用curl命令時,偶爾會遇到一些URL跳轉到新的URL,即HTTP中的3XXredirection,重新導向,如HTTP 302 )。

$curl -s -I $URL > log

這時在返回的報文中,有Location欄位標明了新的URL。

$cat log HTTP/1.1 302 Found Expires: -1 Cache-Control: private, max-age=0Pragma: 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 RequestContent-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_10000000 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.