1 UseLWP::useragent;2 UseHTTP::Cookies;3 4 my $ua= lwp::useragent->new;5 $ua->cookie_jar (http::cookies->New (6File ="Cookies.txt",7AutoSave =1));8 9 my $req= Http::request->new (GET = ="http://www.example.com/");Ten One my $res=$ua->request ($req); A Print $res-Status_line; - #use Http::cookies to create a place where cookies are saved
#或者可以如下所示
1 UseLWP::useragent;2 UseHTTP::Cookies;3 $cookie _jar= http::cookies->New (4File ="/home/perl/cookies.dat",5AutoSave =1,6 );7 $ua->cookir_jar ($cookie _jar);8 #I can do that .9 $ua->cookie_jar ({file ="$ENV {home}/.cookies.txt" });Ten #Note that this is the Cookie_jar parameter requirement is a dictionary
Also, if you want to send a specified cookie together when sending a request, you can:
1 UseLWP::useragent;2 my $ua= lwp::useragent->new;3 4 my $req= Http::request->new (GET = ='http://www.example.com/');5 $req->header ('Cookies'="key1=value1;key2=value2");6 #This header can refer to the Http::headers module7 my $res=$ua->request ($req);8 Print $res->status_line
Lwp::useragent Introduction to Cookie Settings 3