Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to POST to a web server?

by Incognito (Pilgrim)
on Nov 21, 2001 at 02:04 UTC ( [id://126663]=note: print w/replies, xml ) Need Help??


in reply to How to POST to a web server?

I believe what you are trying to do is make a POST to a web page, and then view or store the returned page... This is a straightforward problem and here is the Perl code that will do this:

http_post.pl

#!/usr/bin/perl -w use strict 'subs'; use LWP::UserAgent; BEGIN { $ENV{PATH} = '/usr/bin:/bin' } require LWP::UserAgent; $URL = "http://localhost/Test.asp"; print "Posting to the page '$URL'.\n"; ($returnedPage) = DoPost ($URL); print "Dispaying POSTed results:\n"; print "="x72."\n$returnedPage\n"."="x72."\n"; exit; sub DoPost { local ($url) = @_; use HTTP::Request::Common qw(POST); use LWP::UserAgent; my $browserType = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5. +0)"; my $httpReferer = "http://www.PerlMonks.org"; my $userAgent = new LWP::UserAgent; $userAgent->agent("$browserType "); my $request = POST "$url", [ Value1 => 'Some value for 1', Value2 => 'My value 2 result', Value3 => 'My value 3' ]; $request->referer("$httpReferer"); # Post the request to the server my $response = $userAgent->request($request)->as_string; return ($response); }

Test.asp

Take the code below and rename it "test.asp" off the root of your web server (running IIS) to verify that the contents you POSTed to the web page are in fact being accepted by the page you are submitting to:
This page writes out POSTed FORM values: <P> <% Call WriteQueryStringContents() Sub WriteQueryStringContents Dim strItem Response.Write CHR(10) & "<P>" & CHR(10) Response.Write "QueryString contents:" & CHR(10) For Each strItem In Request.QueryString Response.Write "<LI>" & strItem & ": " Response.Write Request.QueryString(strItem) & "</LI>" & CHR(10 +) Next Response.Write CHR(10) & "<P>" & CHR(10) Response.Write "Form contents:" & CHR(10) For Each strItem In Request.Form Response.Write "<LI>" & strItem & ": " Response.Write Request.Form(strItem) & "</LI>" & CHR(10) Next End Sub %> </P>

Example

Here's the results we get when we run this simple Perl program:
E:\Personal>perl http_post.pl Posting to the page 'http://localhost/Test.asp'. Dispaying POSTed results: ====================================================================== +== HTTP/1.1 200 OK Cache-Control: private Connection: Keep-Alive Date: Tue, 20 Nov 2001 20:54:11 GMT Server: Microsoft-IIS/5.0 Content-Length: 201 Content-Type: text/html Client-Date: Tue, 20 Nov 2001 20:54:11 GMT Client-Peer: 127.0.0.1:80 Set-Cookie: ASPSESSIONIDQGGGQNOK=LEDDBLNAMNEONOJLKHFDFANJ; path=/ This page writes out POSTed FORM values: <P> <P> QueryString contents: <P> Form contents: <LI>Value1: Some value for 1</LI> <LI>Value2: My value 2 result</LI> <LI>Value3: My value 3</LI> </P> ====================================================================== +==
As you can see, the three values we POSTed to the page Test.asp were successfully received and displayed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://126663]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-24 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found