I'm being a bit lazy and just showing you a bit of code i wrote to fetch all
film info from imdb.com and comment on it a bit, to explain what goes on:
### config hash ###
my $href = {
base => 'http://www.imdb.com/',
spage => 'Find',
ua => 'Mozilla/4.74 [en] (Win98; U)',
form => 'select=All&for=',
};
### Set up the content ###
my $content = $href->{form} . $film;
# here, $film is the user input....
### Set up the useragent ###
my $ua = new LWP::UserAgent;
$ua->agent( $href->{ua} );
### Set up the headers ###
my $header = new HTTP::Headers(
'Accept' => 'text/html',
'content-length' => length($content),
'content-type' => 'application/x-www-form-urlencoded',
);
### do the request, get the responce ###
my $req = new HTTP::Request('POST', $url, $header, $content);
my $res = $ua->request($req);
if you now print $res->as_string; you'll find that it holds the entire reply
from the server...
in short, you setup your content as follows (and you can try it if you like by
changing a 'post' to a 'get' on some page and see what is displayed in the
adresbar):
thing1=foo&thing2=bar&thing3=quux etc etc
be sure to define the header properly, as well as the useragent, which above
snippet shows you how to do...
and then it's as simple as doing the last step: do the request, get the
responce...
i hope this example shows you The Path To The Dark Side ;-)
Jos Boumans
Ela Jarecka wrote:
> Thanks, at least I know that I am sending my XML properly.. But I still get
> the same error message, so if anyone has more suggestions
> please write..
>
> Ela
>
> > -----Ursprüngliche Nachricht-----
> > Von: Tim Keefer [mailto:[EMAIL PROTECTED]]
> > Gesendet: Montag, 18. Juni 2001 15:46
> > An: Ela Jarecka; Beginners list (E-Mail)
> > Betreff: Re: Problems with LWP::UserAgent and HTTP::Response
> >
> >
> > Hi Ela,
> > The documentation for perl LWP agent seems sparse. I had a
> > difficult time
> > figuring out how to send multipart form-data. I'll share the
> > code with you that
> >
> > some shared with me. Hope it helps.
> >
> >
> >
> > require LWP;
> > use LWP::UserAgent;
> > use HTTP::Request::Common;
> >
> > # Create a user agent object
> >
> > $ua = new LWP::UserAgent;
> > $ua->agent("AgentName/0.1 " . $ua->agent);
> >
> > # Pass request to the user agent and get a response back
> >
> > my $res = $ua->request (POST $URL, Content_Type =>
> > 'form-data', Content => [
> > login_id => $Username,
> > login_passwd => $Password,
> > name_auth => $Prefix,
> > fname => ["$XML_Dir\\$XML_File"],
> > operation => 'Submit Batch File',
> > ]);
> >
> > # Check the outcome of the response - I guess we just file away
> > if ($res->is_success) {
> > print "success!\n";
> > print $res->content;
> > if ( $res->content =~ /\Q<H2>SUCCESS<\/H2>\E/i ) {
> > print "Deposit successful\n";
> > } else {
> > print POSTLOG "Deposit FAILED.\n";
> > }
> >
> > } else {
> > print " failed!\n";
> > }
> >
> >
> > Ela Jarecka wrote:
> >
> > > Hi,
> > > I am using the following code to send and XML document (
> > output.xml ) to a
> > > remote server:
> > >
> > > use strict;
> > > use LWP::Debug qw(+);
> > > use LWP::UserAgent;
> > > use IO;
> > >
> > > my $resp;
> > > $resp = 'response.xml';
> > > my $FILEH;
> > > open (FILEH, <output.xml>) or die "Can't open file output.xml!\n";
> > >
> > > my $ua = LWP::UserAgent->new;
> > >
> > > #another version that i've tried...
> > > #my $h = new HTTP::Headers Date=> '2001-05-18';
> > > #my $req =
> > > HTTP::Request->new('POST','http://195.252.142.171:8008',$h,$FILEH);
> > >
> > > my $req = HTTP::Request->new(POST => 'http://195.252.142.171:8008');
> > >
> > > #$req->content_type('text/xml');
> > > $req->content($FILEH);
> > >
> > > my $res = $ua->request($req,$resp);
> > <--------------------here I've also
> > > tried plain request($req) but the result is the same
> > > if ( $res->is_success) {
> > > print "OK!\n";
> > > #print $res->as_string;
> > > } else {
> > > print "Failed: ", $res->status_line, "\n";
> > > }
> > >
> > > And that's what I get:
> > >
> > > LWP::UserAgent::new: ()
> > > LWP::UserAgent::request: ()
> > > LWP::UserAgent::simple_request: POST http://195.252.142.171:8008/
> > > LWP::UserAgent::_need_proxy: (http://195.252.142.171:8008/)
> > > LWP::UserAgent::_need_proxy: Not proxied
> > > LWP::Protocol::http::request: ()
> > > LWP::Protocol::http::request: POST / HTTP/1.0
> > > Host: 195.252.142.171:8008
> > > User-Agent: libwww-perl/5.21
> > >
> > > LWP::Protocol::http::request: reading response
> > > LWP::UserAgent::request: Simple result: Internal Server Error
> > > Failed: 500 read timeout
> > >
> > > #######
> > > Could anyone please help me? The problem is that I am not
> > too sure whether
> > > my request is correct in the first place.
> > > In the manuals, $content is described as 'an arbitrary
> > amount of data'.. Is
> > > my filehandle properly interpreted? I've tried
> > > using only the name of the file, but obviously it didn't work, being
> > > interpreted as a 10 chars long string...
> > >
> > > Thanks in advance,
> > > Ela
> >