Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

LWP and HTML 302 Error

by inblosam (Monk)
on May 21, 2002 at 07:08 UTC ( [id://168050]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to login in to a site and then post to a page while still logged in. I can log in fine, but as soon as I try to do the second post I am stuck with an HTTP 1.1/302 error as follows: HTTP/1.1 302 (Found) Object Moved Connection: close Location: defaultpage.cfm?sid=99999999999&CFID=99999&CFTOKEN=9999999 Server: Microsoft-IIS/5.0 Content-Length: 196 Content-Type: text/html Client-Date: Tue, 21 May 2002 06:50:00 GMT Client-Response-Num: 1 Title: Document Moved Object Moved This document may be found here If anyone has any suggestions, maybe even another way to do it, please share. Thanks! My code is as follows (with login/site info taken out):
#!usr/bin/perl -w use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common; my $ua = LWP::UserAgent->new; $ua->cookie_jar(HTTP::Cookies->new(file => 'cookie_jar', autosave =>1) +); my $r = $ua->simple_request(POST "http://www.sitedomain.com/login.cfm" +, { username =>'abcuser', userpass =>'abc123', submit =>'Submit' }); while ($r->is_redirect) { my $u = $r->header('location') or die "missing location: ", $r->as_string; #print "redirecting to $u\n"; $u=~s/^\.+/http\:\/\/www\.sitedomain\.com/s; $r = $ua->simple_request(GET $u); } my $r = $ua->simple_request(POST "http://www.sitedomain.com/page.cfm", { firstname =>"Michael", lastname =>"Jensen", company =>"companyname", address =>"111 East 222 South", city =>"Provo", state =>"UT", zip =>"99999" }); $b = $r->as_string; if ($r->is_success) { $a = "worked"; } else{ $a = "failed"; } print "b is $b";
Michael Jensen InBlosam Productions http://www.inblosam.com

Replies are listed 'Best First'.
(podmaster) Re: LWP and HTML 302 Error
by PodMaster (Abbot) on May 21, 2002 at 10:56 UTC
    Please check the documentation for HTTP::Request::Common.

    The POST method does not take a hashref as an argument. It takes an arrayref instead. ( try  [ ] instead of  { }, see perlref if you don't understand).

    update: oops. apparently it does, not documented at all (i'm kind of a stickler for things like that)

    ###### line 113 in sub form_data in HTTP/Request/Common.pm my @data = ref($data) eq "HASH" ? %$data : @$data; # copy
    update: you say you "can log in fine", how do you know?

    I don't see $cookie_jar->add_cookie_header($request); or $cookie_jar->extract_cookies($response); anywhere. What good is an empty cookie jar, in which you never reach?

    Please check the HTTP::Cookies documentation.

    update:
    I was reviewing some of my nodes, and then I noticed $ua->cookie_jar ( you know, it would've be nice if somebody pointed this out to me). Turning to the documentation would've still been helpfull though (you'd have known the following, and could've dismissed my comments quickly ;)

    $ua->cookie_jar([$cookies]) Get/set the HTTP::Cookies object to use. The default is to have no cookie_jar, i.e. never automatically add ``Cookie'' headers to the requests.

     

    Look ma', I'm on CPAN (and I just saved the day).


    ** The Third rule of perl club is a statement of fact: pod is sexy.
      I know I can log in fine because I can return $b, which was set as $r->as_string, and do so from a browser running the script, so I get the page that shows up when I am logged in.

      What can add_cookie_header($request) or extract_cookies($response) do for me? I have never used those. Thanks for your help and time!
        add_cookie_header($request) and extract_cookie($response) store and extract a cookie from your cookie jar.

        The autosave option only saves the cookie file during destruction.

        You can see the page, because you send the username/password.

        The reason you're having problems the 2nd time, is because when you login, you're issued a cookie, which you simply discard.

        Since you login via Login.cfm, and your 2nd request is to Page.cfm, you must send the cookie you were given by Login.cfm.

        That's how cookie based authentication works. You send your credentials, and get issued a cookie, which you use as credentials istead.

        Now please go read the HTTP::Cookies documentation like I suggested (you could've saved yourself a day or two)

        update:
        I was reviewing some of my nodes, and then I noticed $ua->cookie_jar ( you know, it would've be nice if somebody pointed this out to me). Turning to the documentation would've still been helpfull though (you'd have known the following, and could've dismissed my comments quickly ;)

        $ua->cookie_jar([$cookies]) Get/set the HTTP::Cookies object to use. The default is to have no cookie_jar, i.e. never automatically add ``Cookie'' headers to the requests.

         

        Look ma', I'm on CPAN.


        ** The Third rule of perl club is a statement of fact: pod is sexy.
Re: LWP and HTML 302 Error
by arunhorne (Pilgrim) on May 21, 2002 at 12:23 UTC

    You're using the UserAgent. Why don't you try the following subroutine provided by it:

    $ua->credentials($netloc, $realm, $uname, $pass)

    More info: here (Offsite to CPAN)

    This method should allow you to set your usename and password for the realm you are trying to post to and thus hopefully give you the permissions you need to post.

    Arun

      I don't believe that to be the case.

      He is submitting via CGI (a html form), and basic HTTP authentication is not handled through forms 95% of the time (he's not using basic http authentication).
       

      Look ma', I'm on CPAN.


      ** The Third rule of perl club is a statement of fact: pod is sexy.
Re: LWP and HTML 302 Error
by hagus (Monk) on May 21, 2002 at 13:49 UTC
    Am I missing something, or is this not the issue? If you have a 302, you must obey it and retrieve the new location of the document. Unless of course the 302 is the message generated when a login fails?!

    --
    Ash OS durbatulk, ash OS gimbatul,
    Ash OS thrakatulk, agh burzum-ishi krimpatul!
    Uzg-Microsoft-ishi amal fauthut burguuli.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://168050]
Approved by moodster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-20 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found