Solved

New with libwww-perl and am stuck trying to POST a form.

Posted on 2007-12-01
3
356 Views
Last Modified: 2008-03-04
I am new to libwww perl, and am trying to POST a request, and I am just frustrating myself at this point.

I am trying to write a UA (User Agent) to login to our Barracuda anti-spam box so it can gather some stats.

I can access the initial URL where the login screen is.  I can gather and populate the form fields using HTML::Form.  But when I try to POST the filled out form back, I always get a 302 result code ("Moved").  This happens whether I use the correct or incorrect password.  The 'Content' string LWP is passing back looks the same as the one I grabbed with a packet sniffer when I accessed the system with my browser.

I could just use some help in figuring this out.  Let me know what else would be useful to post and I'll post it.

Thanks!
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTML::Form;
 
my $p;
my $domain       = 'macalester.edu';
my $barracuda    = "barracuda.$domain";
my $cookies      = "/tmp/$barracuda";
my $from         = "fines\@$domain";
my $proto        = 'http';       # change to 'https' if using https
my $url          = "$proto://$barracuda/cgi-bin/index.cgi";
my $user = 'admin';
my $password='secret';
 
$p->{host}       = "$proto://$barracuda";
$p->{timeout}    = 60;
$p->{agent}      = 'LWP 5.808';
my $ua = LWP::UserAgent->new;
$ua->timeout($p->{timeout});
 
my $response = $ua->get($url);
if ($response->is_success) {
    print $response->content;  # or whatever
} else {
    die $response->status_line;
}
print "code is " . $response->code . "\n";
print "message is " . $response->message . "\n";
 
my @forms = HTML::Form->parse($response);
my $form=$forms[0];
print "this form has this many inputs: " . $form->inputs . "\n";
$form->value('user',$user);
$form->value('password',$password);
my $enc_key=$form->value('enc_key');
my $et=$form->value('et');
 
my $request=$form->click;
$request->header(Host => '141.140.1.35');
print "content is " . $request->content . "\n";
my $c=$request->content;
$c .= "&password_entry=";
$request->content($c);
$request->header('Content-length' => length($c));
$request->protocol('HTTP/1.1');
$request->header('User-Agent' => $p->{agent});
$request->header('Accept' => 'text/html');
print "content is " . $request->content . "\n";
print "request is:\n" . $request->as_string . "\n";
$response=$ua->request($request);
print "code is " . $response->code . "\n";
print "message is " . $response->message . "\n";

Open in new window

0
Comment
Question by:mactfines
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 20389746
It might be easier/better to use a different approach/module.

use WWW::Mechanize;
http://search.cpan.org/~petdance/WWW-Mechanize-1.32/lib/WWW/Mechanize.pm
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21046407
Forced accept.

Computer101
EE Admin
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to count occurrences of each item in an array.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question