In a project of mine I need to send a POST statement to a webserver.
I looked at LWP and LWP::UserAgent and didn't quite see what I needed -- I think.
What I ended up doing was making my own Socket connection, and then printing a POST statement to it (code below) and reading in the results.
So fellow monks, based on the code below, did I need to roll my own? I'm sure somebody else can write this networking stuff better than I can.
#!/usr/bin/perl -w
use IO::Socket;
use strict;
my $socket = &new_socket( { host => "foo.com", port => 80 } );
print $socket &new_post( { data => "foobar",
path => "/cgi-bin/foobar.pl",
host => "http://www.foo.com",
referer => "http://www.foo.com/baz.html"
} );
print OUT while( <$socket> );
#########################################
sub new_socket {
my( $args ) = @_;
return IO::Socket::INET->new( PeerAddr => $$args{host},
PeerPort => $$args{port},
Proto => "tcp",
Type => SOCK_STREAM );
}
sub new_post {
my( $args ) = @_;
my $length = length( $$args{data} );
my $post = "POST $$args{path} HTTP/1.0\n";
$post .= "Host: $$args{host}\n";
$post .= "Accept: text/html, text/plain, text/richtext";
$post .= ", text/enriched\n";
$post .= "Accept-Encoding: gzip, compress\n";
$post .= "Accept-Language: en\n";
$post .= "Pragma: no-cache\n";
$post .= "Cache-Control: no-cache\n";
$post .= "User-Agent: Lynx/2.8.3dev.18 libwww-FM/2.14\n";
$post .= "Referer: $$args{referer}\n";
$post .= "Content-type: application/x-www-form-urlencoded\n";
$post .= "Content-length: $length\n\n";
$post .= "$$args{data}\n";
return $post;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.