tags:

views:

218

answers:

2

I am new to Perl and I want to write a Perl program that:

  • creates an HTTP request
  • sends it to any URL (e.g. http://www.google.com )
  • includes a cookie in the request
  • logs the http response codes in a file

I have tried this:

    #!/usr/bin/perl
require HTTP::Request;
require LWP::UserAgent;

$request = HTTP::Request->new(GET => 'http://www.google.com/');
$ua = LWP::UserAgent->new;
$ua->cookie_jar({file => "testcookies.txt",autosave =>1});

$response = $ua->request($request);
if($response->is_success){
print "sucess\n";
print $response->code;
}
else {
print "fail\n";
die $response->code;
}

pls tell how to set cookie in 'request' ie

how to set a cookie when we send HTTP::Request

i was expecting something like:

$request = HTTP::Request->new(GET => 'http://www.google.com/');
$ua = LWP::UserAgent->new;
$ua->new CGI::Cookie(-name=>"myCookie",-value=>"fghij");

is this possible??

+3  A: 

Start with LWP::UserAgent and HTTP::Cookies

David Dorward
LWP::UserAgent shows for HTTP response i want simple http request with set cookie
dexter
How do you expect to record the status code without looking at the response?
David Dorward
i had no clue ,thanks anyways...and what about 'logging the http response codes in a file'
dexter
http://search.cpan.org/perldoc?File::Log
David Dorward
@dexter: The simple way: Open a file and print the response codes into it. The powerful way: Use Log4perl (http://search.cpan.org/perldoc?Log::Log4perl).
Dave Sherohman
edited using Link by David andhttp://kobesearch.cpan.org/htdocs/libwww-perl/HTTP/Request.html#NAME
dexter
+1  A: 

As mentioned cookies are in HTTP::Cookies:

  • You need to create a cookie jar

  • You set the value of the cookies to put in the jar

  • You then associate that jar with your user agent

For example:

my $ua = LWP::UserAgent->new;
my $cookies = HTTP::Cookies->new();
$cookies->set_cookie(0,'cookiename', 'value','/','google.com',80,0,0,86400,0);
$ua->cookie_jar($cookies);
# Now make your request

set_cookie has a rather large number of arguments:

set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest )

This is because the cookie jar is designed from the point of view of a browser (a UserAgent), rather than a single request. This means not all the arguments are so important in this case.

The ones you need to get right are $key, $val, $path, $domain, $port.

Regarding the:

500 Can't connect to www.google.com:80 (Bad hostname 'www.google.com')

It means that LWP can't lookup the address for Google. Are you behind a web proxy? If so you will need to set your proxy in the UA too using something like:

$ua->proxy(['http', 'https'], 'http://proxyhost.my.domain.com:8080/');

Gavin Brock
what does $cookies->set_cookie(0,'cookiename', 'value','/','google.com',80,0,0,86400,0); statement do can you please explain in detail
dexter
added detail on set_cookie - for even more info look at the HTTP::Cookie man page, or Wikipedia's HTTP Cookie page.
Gavin Brock