Find the answer to your Linux question:
Results 1 to 4 of 4
I have a question about using Perl's LWP::UserAgent. I've played around with it enough to understand how to return the source code from a page but now I want to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    31

    Perl LWP::UserAgent


    I have a question about using Perl's LWP::UserAgent. I've played around with it enough to understand how to return the source code from a page but now I want to learn how to actually submit form fields using Perl. I'm assuming that the differences between sites aren't tremendous besides the form's fields names. However after trying quite a few examples I still cannot get it to work. Just for trying purposes I decided to use this site

    www . wheresmycellphone . com

    I believe the source I'm interested in is this

    Code:
    <form action='submit.php' method="post">
    
                <td>
                    <input name="txtisdcode" type="hidden"  value="1" /><h1>1</h1>
               </td>
                <td>
                    <input name="txtcitycode" type="text" class="h1" size="3" />
                </td>
                <td>
                   <input name="txtphone" type="text" class="h1" size="7" maxlength="8" font="terminal" />
    
                </td>
                <td><input name="submitcall" type="submit" class="h2" value="Make it Ring!"/></td>
              </tr>
    And here is my current code, which isn't working.

    Code:
    #!/usr/bin/perl -w
    
    use strict;
    use LWP::UserAgent;
    use HTTP::Request::Common qw(POST);
    
    my $browser = LWP::UserAgent->new;
    my $resp = (POST 'www . wheresmycellphone . com/', ['txtisdcode' => 1,  txtcitycode' => '000', 'txtphone' => '0000000']);
    
    my $response_to_discard = $browser->request($resp);
    print $response_to_discard;
    Obviously I've changed the phone number, and also due to not being allowed to post urls yet I put spaces between the parts, but besides that this is my exact code. It appears to execute fine, but I never receive the call which I am expecting, which I do if I actually use the form on there site. Any ideas on what I am doing wrong.

    Thanks,
    John

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    You are actually using the "request" method incorrectly. This method takes an HTTP::Request object as an argument, not simply a list of parameters.

    I would suggest using the post() method instead, and see how that works.

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    31
    I tried changing my code to use the POST method like you said but I still cannot get it to work. Any ideas on where I'm going wrong?

    Code:
    #!/usr/bin/perl -w
    
    use strict;
    use LWP::UserAgent;
    
    my $browser = LWP::UserAgent->new;
    my $resp = HTTP::Request->new(POST => 'http://www.wheresmycellphone.com/');
    $resp->content_type('text/html');
    $resp->content('txtisdcode=1&txtcitycode=000&txtphone=000000');
    print $browser->request($resp)->as_string

    Thanks,
    John

  4. #4
    Just Joined!
    Join Date
    Mar 2009
    Posts
    31
    I know this is somewhat an old topic, but in case anyone else looks for the solution to this problem here it is.

    Code:
    #!/usr/bin/perl -w
    
    use strict;
    use LWP::UserAgent;
    
    my $browser = LWP::UserAgent->new;
    
    my @req = ('http://www.wheresmycellphone.com/submit.php',
               [txtisdcode => 1,  txtcitycode => '000', txtphone => '0000000', subm$
    
    my $response_to_discard = $browser->post(@req);
    print $response_to_discard->as_string();
    - John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •