Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Getting a Cookie from .net iis server

by benny666 (Initiate)
on Oct 27, 2003 at 01:30 UTC ( [id://302305] : perlquestion . print w/replies, xml ) Need Help??

benny666 has asked for the wisdom of the Perl Monks concerning the following question:


I have got this script to automate submitting a form to a website and getting the response back.
only this site is powered by .net and when I use user agent to get even the home page it is allways getting an
error and redirecting to an error page asperrorpath=...

I found out the if I turn of the cookies I get this message all the time - meaning that the site is storing id
and session on my computer as a cookie.

so I added the cookie functionality to the user agent but I can't seem to have the site store the
cookies on any cookie folder not the cutom lwp_cookies (see script)
and not he default microsoft cookie folder.

any suggestions???

use LWP::UserAgent; use HTTP::Request::Common qw(POST); use HTTP::Cookies; use HTTP::Cookies::Microsoft; BEGIN { $LWP::DebugFile::outname = 'c:/ua_debug.txt' } use LWP::DebugFile qw(+);
_____ tried that
#create cookies jar to hold the cookies for this user agent my $cookie_jar = HTTP::Cookies->new( file => "c:/MyFolder/Cookies/lwp_c +ookies.dat", autosave => 1, );
_____and also that
#create cookies jar to hold the cookies for this user agent my $cookie_jar = HTTP::Cookies::Microsoft->new( file => "c:/Documents and Settings/owner/Cookie +s/index.dat", 'delayload' => 0, ); my $ua; #user agent $ua = LWP::UserAgent->new; $ua->cookie_jar($cookie_jar); $curUrl =""; print "getting $curUrl\n"; @curContent = newGetRequest($curUrl); sub newGetRequest() { my $myUrl; my $myRes; my $myReq; # get url to create a request from $myUrl=@_[0]; # Create a request $myReq = HTTP::Request->new(GET =>"$myUrl"); #pass any cookies that exsists with this request $cookie_jar->add_cookie_header($myReq); # pass request to user agents $myRes = $ua->send_request($myReq); # if response put some cookies get them $myRes->base(""); $cookie_jar->extract_cookies($myRes); # get cookies put by the site if ($myRes->is_success) { print "Success in GET $myUrl" ; return $myRes->content; }else { print "Fail in GET $myUrl"; } }

Edit by thelenm: added code tags

Replies are listed 'Best First'.
Re: Getting a Cookie from .net iis server
by tachyon (Chancellor) on Oct 27, 2003 at 05:58 UTC

    This is how we do it with an OO LWP wrapper we use, hope it helps:

    sub new { my $class = shift; my %args = @_; my $self = { _ua => LWP::Custom->new() }; bless $self, $class; # check for passed args to modify defaults $self->{cookie_dir} = $args{cookie_dir} ? $args{cookie_dir} : '/tm +p'; # pretend to be IE6 by default ;-) $self->{agent} = $args{agent} ? $args{agent} : 'Mozilla/4.0 (compa +tible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)'; # use a 30 secont timout by default $self->{timeout} = $args{timeout} ? $args{timeout} : 30; # meta refresh threshold $self->{meta_threshold} = $args{meta_threshold} ? $args{meta_thres +hold} : 15; # support cookies, first make a unique cookie file for this instan +ce my ( $fh, $name ) = get_tempfile ( $self->{cookie_dir}, 'cookie' ) +; # as we have created the file we need to make it look like a cooki +e file # so that LWP does not winge about the missing header print $fh "#LWP-Cookies-1.0\n"; close $fh; $self->{cookie_file} = "$self->{cookie_dir}/$name"; # set up the cookie jar $self->{_cookie_jar} = HTTP::Cookies->new( file => $self->{cookie_fil +e}, autosave => 0, ignore_discard => 1 ); $self->{_cookie_jar}->load( $self->{cookie_file} ); $self->{_ua}->agent( $self->{agent} ); $self->{_ua}->timeout( $self->{timeout} ); return $self; } # this is the simplified guts of the fetch method: my $request = HTTP::Request->new( $method, $url ); # this call can choke, an example is which meta + # refreshes to aol://1722:ukshopping causing this call to explode eval{$self->{_cookie_jar}->add_cookie_header($request)}; my $response = $self->{_ua}->request($request); $self->{_cookie_jar}->extract_cookies($response); $self->{_cookie_jar}->save( $self->{cookie_file} ); $obj->{content} = $response->content; $obj->{code} = $response->code; $obj->{message} = $response->message; $obj->{title} = $response->headers->title; # we customize the LWP::UserAgent class to suit our needs package LWP::Custom; use base 'LWP::UserAgent'; # add a set_basic_credentials method, using a closure to remember { my ( $username, $password ); sub set_basic_credentials{ ( $username, $password ) = @_[1..2] } sub get_basic_credentials{ $username, $password }; }