Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using LWP 1

Status
Not open for further replies.

vcherubini

Programmer
May 29, 2000
527
US
Hello:

I have a question about how to use the module LWP. I recently read the post "LWP:UserAgent is mondo Cool!", and got an idea.

I have a page on my server that looks like the follow:

[tt]
<form action= method=post>
<input type=text name=logstring>
<input type=text name=logpw>
<input type=hidden name=localpage value=default.asp>
<input type=submit>
</form>
[/tt]

From a Perl script, I would like to fill out the values of the text fields (I would assume with LWP::UserAgent?) and then submit the page. So that page.asp on gets executed.

Then, from the output from otherserver.com, I can parse that and see if I get the correct data. That part I can do, but the other part is perplexing me.

Any and all help is appreciated.

Thanks.

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
If the .asp page will accept a 'GET', you could simply construct the URI you want and get ($uri);

Sorry, I don't have time at the moment to dig into LWP.

HTH


keep the rudder amid ship and beware the odd typo
 
You can also use the URI::URL module to construct a POST request and use LWP::UserAgent to send it and get the results. First you must build a hash with the key/value pairs and pass it to the url query_form function. Here's an example:
Code:
use URI::URL;
use LWP::UserAgent;
my $ua = new LWP::UserAgent;

# Build the &quot;form&quot;
%form = (name => 'My Name', bday => '02/20/1956' );
my $tmpurl = url(&quot;http:&quot;);
$tmpurl->query_form(%form);

my $req = new HTTP::Request 'POST', &quot;[URL unfurl="true"]http://www.yoururl.com/cgi-bin/yourprog.pl&quot;;[/URL]
$req->content_type('application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
$req->content($tmpurl->equery);
my $res = $ua->request($req);
if ( $res->is_success ) {
   print &quot;Program returned: &quot;,$res->as_string;
} else {
   print &quot;HTTP error: &quot; . $res->code . &quot; &quot; . $res->message;
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Tracy:

Thanks for that, seems to do just what I need it to.

goBoating:

Thanks for the speedy reply, but unfortunately, the page requires the method to be post. Sorry.
Thanks again,

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Hello
I would like to ask you something since you seem to be more experience than me.

I want to log on to mail.yahoo.com, provide my user/pass and check my mail.

I do not undestand what is hte difference between using the LPW::USerAgent and URI::URL.

If I know where the fields are in the html form is it possible to use the LPW::USerAgent to query the form or do I need Both..




thanks a lot
George
 
URI::URL and LWP::UserAgent are used for two different purposes, although they can work together. URI::URL is used for manipulating URLs. LWP::UserAgent is used for actually sending and recieving http requests/responses.

You don't need to know WHERE the fields are in the form, just what their NAMES are. You also need to know the URL the form is submitted to (in the ACTION= part of the form tag). The you can construct a parameter string with the form fields and call the cgi program. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
By the way, thanks for resurrecting this thread! I had just told someone in the Javascript forum to do a search here for this thread or one like it. Then I clicked over to this forum, and here it was, back at the top of the list! Got to be the most interesting coincidence so far this year!
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top