ActiveState Powered by ActiveState

ActiveState Community


Set-Cookie header

Posted by shamly on 2007-12-03 05:10

Hi,

I have an application that needs to send a HTTP request to a web server. The first request is a login request where all the params are sent via GET method. The reply to this request will be a session ID. Next time i make a request to this URL i need to set this session id in Set-Cookie header or else an error msg will be returned. I have tried doing it in the following manner ..

use HTTP::Request::Common qw(GET);
use LWP::UserAgent;
use HTTP::Cookies;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);

my $cookie_jar = HTTP::Cookies->new;
my $req = HTTP::Request->new(GET => "http://remote_ip:port/file_name.jsp?get_bal=shamly");

$cookie_jar->set_cookie("JSESSIONID=FFX1A19D2699C0099");

$ua->cookie_jar($cookie_jar);

my $res = $ua->request($req);

if ($res->is_success) {
# print $res->decoded_content;
print $res->as_string;
}
else {
print "Error: " . $res->status_line . "\n";
}

The request seems to fail as an error code is returned. Another probs is that I have no access on the server side. At present I am using a php file using curl functions.

curl_setopt ($ch, CURLOPT_COOKIE, "JSESSIONID=FFX1A19D2699C0099");

I would like to be able to implement this in perl itself. Any help please ...

gangabass | Mon, 2007-12-03 18:12

Life is short so "use WWW::Mechanize".
You only need to make first login request after that your cookies will be automatically set.
By the way you can make the same with plain LWP::UserAgent (so you don't need HTTP::Request::Common and HTTP::Cookies). Just use same $ua for both requests.

shamly | Tue, 2007-12-04 00:58

I tried the URL calls from separate perl scripts earlier. Probebly thats why it didn't work so I guess it will work if I did the whole thing in one go. Will check using plain LWP::UserAgent first. Thanks

gangabass | Tue, 2007-12-04 02:20

Of course this must be one script (unless you save your cookies to file in one script and load them in another). See $cookie_jar->save() and $cookie_jar->load() in HTTP::Cookies.

-->