Handle HTTP 302 in Linux curl command

Source: Internet
Author: User

LinuxWhen the curl command is used in the system, sometimes some URLs jump to the new URL, namely 3 XXredirection in HTTP, redirection, suchHTTP 302).

$curl -s -I $URL > log

In the returned message, the Location field indicates the new 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

When I try to get this new URL and then perform curl, HTTP 400 (Bad Request, incorrect Request) is always reported ).

$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

Print the new URL directly, copy and paste the URL, but there is no problem.

$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

Think twice!

Last. It is found that the new $ URL is not exactly the same as "http: // 182.118.2.11/youku/5571865A52945820CFCE162719/Hangzhou". The $ URl end with an additional '0a' line break.

$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

Remove this line break. Everything is OK.

URL=`cat log | grep '^Location' | cut -f2 -d ' ' |sed 's/\r//' ` Been dazed and confused for so long

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.