简体   繁体   中英

Perl “HTTP::cookies”“ with ”LWP::UserAgent“ error: ”does not look like a Netscape cookies file"

I'm trying to do the equivalent of curl --cookie cookies.txt "https://foo.example.org/bar" in a Perl script. I have a cookies file that I believe is in Netscape format (tab delimited) that works fine with curl . HTTP::cookies complains:

cookies.txt does not look like a Netscape cookies file at /System/Library/Perl/Extras/5.10.0/HTTP/Cookies/netscape.pm line 21, line 1.

My script is just a stub for testing:

#!/usr/bin/perl -w

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
use HTTP::Cookies::Netscape;
my $cookie_jar = HTTP::Cookies::Netscape->new
                  ( 'file' => 'cookies.txt', );  
my $url = 'http://www.google.com/';
my $response = $ua->get( $url );
print "Return: $response->status_line\n";

The cookies.txt file actually starts with a comment:

# This content may be pasted into a cookies.txt file and used by wget

But I tried deleting the comment and got the same exact error. The cookies.txt file has Unix style line endings.

Arg!

Turns out that, while curl is happy with the file, the HTTP::Cookes::Netscape module wants the first line to be

# Netscape HTTP Cookie File

I added that, and now it seems happy.

(I found the module source code and searched for the error message.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM