List Info

Thread: HTTP request arguments/content




HTTP request arguments/content
user name
2007-01-29 08:49:38

Hello,

 

I have been trying to open a html page in perl. It works but I can’t send the correct arguments (post values) with it to work.

 

Here is the code:

 

[begin snippet]

my $browser = LWP::UserAgent->new;

sub get_page_http($$$$$;$){

  my $browser  = shift;

  my $remote  = shift || 'localhost';

  my $path ;   = shift;

  my $leverancier_nr = shift;

  my $FMnummer = shift;

  my $datastring = shift || '';#### raw datastring

 

  my $res;

 

  log_print "get_page: $remote:$path ($datastring)n";

 

  my $ua = LWP::UserAgent->new();

  my $url = new URI::URL($remote.$path);

  <some code to define the content length>

  my $hdrs = new HTTP::Headers('Content-Length' => $content_length);

  my arg = ('submit_waarde' =&gt; '', 'page' => 'zks1', 'submit_waarde' => '', 'leverancier_nr' => $leverancier_nr, 'FMnummer' => $FMnummer);

  my $retry=1;

  while($retry){

 &nbsp;  my $formpost= HTTP::Request->new('POST', $url, $hdrs, arg);

$res = $browser->request($formpost);

   &nbsp; &nbsp; &nbsp;  if($res-&gt;is_error){

   &nbsp; &nbsp; &nbsp;   &nbsp;$retry++;

   &nbsp; &nbsp; &nbsp;   &nbsp;if($retry>2){

   &nbsp; &nbsp; &nbsp;   &nbsp; &nbsp;$retry=0;

   &nbsp; &nbsp; &nbsp;   &nbsp; &nbsp;####only try twice

   &nbsp; &nbsp; &nbsp;   &nbsp;}

   &nbsp; &nbsp; &nbsp;  }

   &nbsp; &nbsp; &nbsp;  $retry=0;

  }

  if (wantarray){

 &nbsp;  #my($header,$html_body)=split /$BLANK/,$res->content,2;

 &nbsp;  return ($res->headers,$res-&gt;content);

  }else{

 &nbsp;  return $res->content;

  }

}

[end snippet]

 

The problem is that I can not get the post arguments correctly. I have been trying to put them in the header of using message in the HTTP::Message class.

  …

 &nbsp;my $mssg = HTTP::Message->new;

  …

 

Can you tell me how to post the post values?

 

Greetings,

 

Marijn Pessers

 

Re: HTTP request arguments/content
user name
2007-01-29 15:55:05
Marijn Pessers wrote:
> Hello,
> 
>  
> 
> I have been trying to open a html page in perl. It
works but I can’t 
> send the correct arguments (post values) with it to
work.

Try this one:

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);

sub get_page_http ($$$$$;$);

my $ua = LWP::UserAgent->new;
my $remote = 'localhost';	# fix me
my $path = '/somecgi.pl';	# fix me
my $leverancier_nr = 'stuff';
my $FMnummer = 'more stuff';
my $datastring = 'what goes here?';

my ($hdr, $ct) = get_page_http ($ua, $remote, $path,
$leverancier_nr,
   $FMnummer, $datastring);
print "nhdrs:n$hdrnct:n$ctn";
exit;

sub get_page_http ($$$$$;$) {
	my $ua = shift;
	my $remote = shift || 'localhost';
	my $path = shift || '/';
	my $leverancier_nr = shift;
	my $FMnummer = shift;
	my $datastring = shift || '';	# raw datastring for ?????

# print "get_page: http:$remote$path
('$datastring')n";

my $url = "http://$remote$path";
my $req = POST $url, Content => [
     'submit_waarde' => '', 'page' => 'zks1',
'submit_waarde' => '',
     'leverancier_nr' => $leverancier_nr, 'FMnummer'
=> $FMnummer ];

for (1 .. 2) {
	my $res = $ua->request($req);
	if ($res->is_error) {
		sleep 1;	# wait a sec and try again
		next;
	}
	if (wantarray) {
		return ($res->headers_as_string, $res->content);
	} else {
		return $res->content;
	}
}

# failed return null string(s)

if (wantarray) {
	return ('', '');
} else {
	return '';
}

}

__END__
_______________________________________________
Perl-Win32-Web mailing list
Perl-Win32-Weblistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )