CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[SOLVED] Using Perl Script from a Webpage to POST HTTP Data

 



SteffenBaier
Novice

Jun 17, 2009, 5:16 AM

Post #1 of 2 (714 views)
[SOLVED] Using Perl Script from a Webpage to POST HTTP Data Can't Post

Hi,

I am trying to create a Perl Script that enable me using a Web Page running on Xampplite to POST Information to a Micro Browser running on a Polycom Phone.

I have absolutely no Perl Knowledge but have already a basic working Perl Script that can be called from a Windows Command Line.

Working Example:


Code
   #!c:/xampplite/perl/bin/perl.exe   
use strict;
use warnings; # *** Perl distribution libwww-perl is REQUIRED ***
require LWP::UserAgent;



if (scalar @ARGV < 5) {
print "\nSends an authenticated POST request, containing a URI to fetch, to an IP phone.\n\n";
print "Usage:\n";
print "digest-uripush-ua.pl <auth_realm> <auth_user> <auth_pw> <phone_ip> <relative_uri> [<priority>]\n";
print "\n";
print "e.g. digest-uripush-ua.pl \"PUSH Authentication\" JoeUser MyPassword 172.24.44.135 apps/apptest.plcm critical\n";
print " Sends a POST to 172.24.44.135 with content <URI priority=\"critical\">apps/apptest.plcm</URI>\n\n";
exit;
}
my $realm = $ARGV[0];
my $user = $ARGV[1];
my $pw = $ARGV[2];
my $phoneip = $ARGV[3];
my $reluri = $ARGV[4];
my $priority = 0;
if (defined $ARGV[5]) {
$priority = $ARGV[5];
}



print "\nAuthentication Realm: ", $realm;
print "\nAuthentication Username: ", $user;
print "\nAuthentication Password: ", $pw;
print "\nTarget Phone IP: ", $phoneip;
print "\nPush URI: ", $reluri;
print "\nPriority: ";
if ($priority) {
print $priority;
} else {
print '<default>';
}
print "\n\n";




my $phoneuri = 'http://'.$phoneip.'/push';



my $httpcontent = '<PolycomIPPhone><Data';
if ($priority) {
$httpcontent = $httpcontent.' priority="'.$priority.'"';
}
$httpcontent = $httpcontent.'>'.$reluri."</Data></PolycomIPPhone>\n";



print "Push Content:\n".$httpcontent."\n";
print "\nSending POST to ", $phoneuri, "\n";



my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->credentials($phoneip.':80', $realm, $user, $pw);



my $response = $ua->post($phoneuri, Content => $httpcontent);

print "- Response from host: ------------------------------------\n";
print $response->as_string;
print "----------------------------------------------------------\n";



exit;



above would be called like this:


Code
   digest-uripush-data.pl "PUSH Authentication" test ge1c0 192.168.253.100 Hello! critical



and output this:


Code
   Authentication Realm: PUSH Authentication   
Authentication Username: test
Authentication Password: ge1c0
Target Phone IP: 192.168.253.100
Push URI: Hello!
Priority: critical Push Content:
<PolycomIPPhone><Data priority="critical">Hello!</Data></PolycomIPPhone>




Sending POST to http://192.168.253.100/push
- Response from host: ------------------------------------
HTTP/1.1 200 OK
Connection: keep-alive
Date: WED, 17 JUN 2009 11:24:12 GMT
Server: Polycom SoundPoint IP Telephone HTTPd
Content-Length: 43
Content-Type: text/html
Client-Date: Wed, 17 Jun 2009 11:24:16 GMT
Client-Peer: 192.168.253.100:80
Client-Response-Num: 1



Push Message will be displayed successfully
----------------------------------------------------------





My Attempt:

I collect the $request{'realm'} etc Data via a Web Page (Example.html attached) and then parse them to the Perl Script (Test.pl attached)


Code
   

#!c:/xampplite/perl/bin/perl.exe

print "Content-type: text/html\n\n";
&getFormData;
print "<h2>Here are all the Details that need submitting:</h2>";
print "<br>\n";
print "Realm: $request{'realm'}";
print "<br>\n";
print "Username: $request{'user'}";
print "<br>\n";
print "Password: $request{'pw'}";
print "<br>\n";
print "IP Address: $request{'ip'}";
print "<br>\n";
print "Text: $request{'reluri'}";
print "<br>\n";
print "Importance: $request{'priority'}";
print "<br>\n";
print "\n\n";

my $phoneuri = 'http://'.$request{'ip'}.'/push';
my $httpcontent = '<PolycomIPPhone><Data priority="'.$request{'priority'}.'">'.$request{'reluri'}.'</Data></PolycomIPPhone>';

print "<br>\n";
print "Push Content:\n".$httpcontent."\n";
print "<br>\n";
print "\nSending POST to ", $phoneuri, "\n";



require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->credentials($request{'ip'}.':80', $request{'realm'}, $request{'user'}, $request{'pw'});
exit;

# Subroutine below this line.

sub getFormData {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\n/ /g;
$request{$name} = $value;
}
}



Will return this on a Web Page the script creates in order to check the Output:


Code
   

Here are all the Details that need submitting:

Realm: PUSH Authentication
Username: chad
Password: ge1c0
IP Address: 192.168.253.100
Text: Hello!
Importance: critical

Push Content: Hello!
Sending POST to http://192.168.253.100/push

As you can see in above the Push Content only shows the $request{'reluri'} and has not joined the $request{'priority'} to it.

It also does not POST the Data to the IP Address specified.

In the Beginning I only had xampplite installed and I now added the PERL add on as an error:

Can't locate LWP/UserAgent.pm in @INC (@INC contains: .)

pointed towards a not fully Installed LWP Packet.

Could anyone help me with above?



THX



Steffen


(This post was edited by SteffenBaier on Jun 17, 2009, 6:00 AM)
Attachments: test.pl (1.31 KB)
  example.html (0.67 KB)


SteffenBaier
Novice

Jun 17, 2009, 5:58 AM

Post #2 of 2 (711 views)
Re: [SteffenBaier] [SOLVED] Using Perl Script from a Webpage tp POST HTTP Data [In reply to] Can't Post

Dear all,

looking through the code of the original Perl script again I simply added behind:

$ua->credentials($request{'ip'}.':80', $request{'realm'}, $request{'user'}, $request{'pw'});


Code
 my $response = $ua->post($phoneuri, Content => $httpcontent);print "- Response from host: ------------------------------------\n"; 
print $response->as_string;
print "----------------------------------------------------------\n";



And as a Result the new Opened Web Page now shows:

Push Content: Hello!
Sending POST to http://10.252.36.107/push - Response from host: ------------------------------------ HTTP/1.1 200 OK Connection: keep-alive Date: WED, 17 JUN 2009 12:54:44 GMT Server: Polycom SoundPoint IP Telephone HTTPd Content-Length: 43 Content-Type: text/html Client-Date: Wed, 17 Jun 2009 12:54:49 GMT Client-Peer: 10.252.36.107:80 Client-Response-Num: 1 Push Message will be displayed successfully ----------------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives