I don't think you understand how the cookie_jar is supposed to be used.


========
From the LWP::UserAgent docs:

$ua->cookie_jar([$cookie_jar_obj])


Get/set the cookie jar object to use. The only requirement is that the cookie jar object must implement the extract_cookies($request) and add_cookie_header($response) methods. These methods will then be invoked by the user agent as requests are sent and responses are received. Normally this will be a HTTP::Cookies object or some subclass.
========


The LWP::UserAgent object will handle extracting and adding cookies for any requests.
You don't need to do any of this.


The more fundamental problem is that you don't seem to understand how the web page works.

Perhaps the cookie returned by the first web page contains some relevant data, but there's a lot more to submitting a form than just making sure the cookie is correct.

When you submit a form, the web page will pass back data to the web server depending on the user's input.

Your code takes the original request URL and does a POST request with any cookies returned from the first GET request. The web server is expecting more data than that.

I suggest reading up on HTML forms and the HTML::Form module.

J


From: "tv fw" <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Subject: Submitting a form to a website
Date: Fri, 31 Oct 2003 12:50:31 +0000

Hello

I am trying to query a website by submitting a form using LWP user agents and cookies
this site is using ASP.NET and IIS 5.0 and is installing cookies on the clients if I turn off the cookies
the site can't be browsed at all.


the problem is that when ever I try to send a request to the site I can't seem to get the user agent
to recieve the cookies and save it into the cookie_jar thus I get a redirect page from the server
with an error code 302 page found but object was mooved (in asp it is something like
?asperrorpath=/MainSite/Default.aspx


here is the code that I am using:


#!perl


#script uses an option to submit a get request and a post request
#I will show both options

#user agent object to create a request and response
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use HTTP::Cookies;
use HTTP::Cookies::Microsoft;


# this section use LWP:Debug file to create a ua_debug.txt file that will log the


actions of the user agent
BEGIN { $LWP::DebugFile::outname = 'c:/Marketing/bin/perl/ua_debug.txt' }
#use LWP::DebugFile;
use LWP::DebugFile qw(+); # with verbouse option

use strict;
use CGI::Carp qw( fatalsToBrowser );
use Time::localtime;
use File::Basename;


#First Try #create cookies jar to hold the cookies for this user agent #my $cookie_jar = HTTP::Cookies->new( # file =>

"c:/Marketing/RealEstate/Cookies/lwp_cookies.dat",
#                                    autosave => 1,
#
#                                   );

#second Try
my $cookie_jar = HTTP::Cookies::Microsoft->new(
file => "c:/Documents and Settings/BIGR2D2/Cookies/index.dat",
'delayload' => 0,
);



my $ua; #user agent $ua = LWP::UserAgent->new; $ua->cookie_jar($cookie_jar);


sub submitUrlMgr() {



my $curUrl ="http://www.elliman.com/Default.aspx";; print "getting $curUrl\n"; my @curContent = newGetRequest($curUrl);

print @curContent;


$curUrl = "http://www.elliman.com/Default.aspx";; @curContent = newPostRequest($curUrl,"elliman");

print @curContent;


}




#functions

sub newGetRequest() {

 my $myUrl;
 my $myRes;
 my $myReq;

 # get url to create a request from
 [EMAIL PROTECTED];

 # 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 agent
 $myRes = $ua->send_request($myReq);

 # if response has cookies get them
 $myRes->base("http://elliman.com/";);
 $cookie_jar->extract_cookies($myRes);  # get cookies put by the site

    if ($myRes->is_success) {
         printToLog("Success in GET $myUrl");
          return $myRes->content;
    }else
        {
         printToLog("Fail in GET $myUrl");
         }

}



sub newPostRequest() {

 my $myUrl;
 my $myCompanyParams;
 my $myRes;
 my $myReq;
 my $myDate;

 # get url to create a request from
 [EMAIL PROTECTED];

 # get parameters for the form
 $myCompanyParams = @_[1];


if ($myCompanyParams =~ /elliman/ ) {


   print "post elliman\n";
   $myReq = POST $myUrl  ;
 }


#pass any cookies that exsists with this request $cookie_jar->add_cookie_header($myReq);


# Pass request to the user agent and get a response back $myRes = $ua->send_request($myReq);

 $myRes->base("http://elliman.com/";);
 # if response has cookies get them
 $cookie_jar->extract_cookies($myRes);  # get cookies put by the site

    if ($myRes->is_success) {
          print "Success in POST $myUrl";
          return $myRes->content;
    }else
        {

print "Fail in POST $myUrl";

}

}

benny

_________________________________________________________________
Surf and talk on the phone at the same time with broadband Internet access. Get high-speed for as low as $29.95/month (depending on the local service providers in your area). https://broadband.msn.com



_________________________________________________________________
Concerned that messages may bounce because your Hotmail account has exceeded its 2MB storage limit? Get Hotmail Extra Storage! http://join.msn.com/?PAGE=features/es




Reply via email to