#!/usr/bin/perluse LWP;$useragent = LWP::UserAgent->new;$request = new HTTP::Request('GET',"http://www.example.com");$response = $useragent->simple_request($request);print $response->as_string();
Date: Mon, 14 Apr 2003 18:38:28 GMTServer: GWS/2.0Content-Length: 2691Content-Type: text/htmlContent-Type: text/html; charset=ISO-8859-1Client-Date: Mon, 14 Apr 2003 18:38:29 GMTClient-Peer: 216.239.57.99:80Client-Response-Num: 1Connection: CloseSet-Cookie: PREF=ID=48fd767576ebd920:TM=1050345508:LM=1050345508:S=qLA8i5XyvLX37lG6;expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.comTitle: Google
$cookie_jar = HTTP::Cookies->new;
$cookie_jar->extract_cookies($response);
$cookie_jar->add_cookie_header($request);
#!/usr/bin/perluse LWP;use HTTP::Cookies;# construct objects$useragent = LWP::UserAgent->new;$cookie_jar = HTTP::Cookies->new;# send request for main Google page$request = new HTTP::Request('GET',"http://www.google.com");$response = $useragent->simple_request($request);# extract cookie from response header$cookie_jar->extract_cookies($response);# set user preference on Google to Spanish language$request = new HTTP::Request('GET',"http://www.google.com/setprefs? submit2=Save+Preferences+&hl=es<=all&safe=images&num=10 &q=&prev=http%3A%2F%2Fwww.google.com%2F&ie=UTF-8&oe=UTF-8");$cookie_jar->add_cookie_header($request);$response = $useragent->simple_request($request);# extract new cookie from response header$cookie_jar->extract_cookies($response);# send request for main Google page (will return Spanish Google page) $request = new HTTP::Request('GET',"http://www.google.com");$cookie_jar->add_cookie_header($request);$response = $useragent->simple_request($request);print $response->as_string; # print response body to verify cookies work (some text now in spanish)
Copyright © 2006-2008 Corey Goldberg
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.