Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Add HTTP Post For Socket Connection

by randallc59 (Initiate)
on Nov 06, 2012 at 22:27 UTC ( #1002592=perlquestion: print w/ replies, xml ) Need Help??
randallc59 has asked for the wisdom of the Perl Monks concerning the following question:

I need some help adding HTTP Post to an existing code. Need to add the following code: POST /cbridge/xmlhost HTTP/1.1 HOST: 127.0.0.1 Content- length: 357

#!/usr/bin/perl use strict; use IO::Socket; use DBI; use DBD::Oracle; our (@command, $command); our ($socket_success_fl); our $svcpk; my $cable_command; ##$cable_command = ' ##<Transaction id = "12345" wait="1" priority="1"> ##<AddUpdateLocation> ##<LocationId> 8002721493 </LocationId> ##<HeadendCode> 01 </HeadendCode> ##<Address1> 19 Bow Circle </Address1> ##<Address2> Bow Circle Business Ctr Unit 402 </Address2> ##<City> Hilton Head Island</City> ##<State> SC </State> ##<Zip> 29926 </Zip> ##</AddUpdateLocation> ##</Transaction>'; ####$cable_command = $ARGV[0] #### or die "Could not find an argument \n"; print "$cable_command\n"; $socket_success_fl = 'N'; socket_connect(); if ($socket_success_fl =~ /Y/){ process_command(); socket_close(); }elsif ($socket_success_fl =~ /N/){ print "Unable to connect to socket!!....\n"; } sub socket_connect { socket (SH, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die $! +; my $dest = sockaddr_in (22928, inet_aton('172.17.15.102')); connect (SH, $dest) || die $!; $socket_success_fl = 'Y'; print "Connected to socket\n"; } sub process_command { my $v_param; my $buffer; $v_param = $cable_command; @command = split(/&/,$v_param); foreach $command (@command) { print "$command\n"; if ($command =~ /SVCPK=/ ) { $svcpk = substr($command,6,10); print "svcpk = ", $svcpk, " \n"; } else { print "About to send command\n"; send (SH, "$command\r", 0); print "sent command\n"; print "about to read response buffer\n"; sysread (SH, $buffer, 1000); print "read response buffer\n"; print "response = ", $buffer, " \n"; } } } sub socket_close{ close(SH); print "Socket closed \n";

Comment on Add HTTP Post For Socket Connection
Download Code
Re: Add HTTP Post For Socket Connection
by flexvault (Parson) on Nov 07, 2012 at 14:22 UTC

    Welcome randallc59,

    I looked at this yesterday, and tried to format your code, but it just takes too long and I wasn't sure I was doing it correctly. While Perl doesn't have a problem with line and space formatting, human monks do.

    I suspect that you could copy and paste from a *nix terminal to the HTML to ask this question, but *nix uses "\n" for line termination and HTML needs "\r\n" within the code.

    Also <code> should end with </code>

    Try it and verify that it looks correct. That's what the 'preview' button if for...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: Add HTTP Post For Socket Connection
by flexvault (Parson) on Nov 07, 2012 at 21:11 UTC

    Well done Perlbotics,

    Thanks to a suggestion from Perlbotics who said "Re Re: Add HTTP Post For Socket Connection Hi, simple trick - unless you were joking ;-) (Right-?)Click your browsers "Show Sourcecoude" or "View Page Source" option and scroll to the posting. Most of the time, the copy/pasted stuff is readable there. Regards, PB" and it worked!

    #!/usr/bin/perl use strict; use IO::Socket; use DBI; use DBD::Oracle; our (@command, $command); our ($socket_success_fl); our $svcpk; my $cable_command; ##$cable_command = ' ##&lt;Transaction id = "12345" wait="1" priority="1"> ##&lt;AddUpdateLocation> ##&lt;LocationId> 8002721493 &lt;/LocationId> ##&lt;HeadendCode> 01 &lt;/HeadendCode> ##&lt;Address1> 19 Bow Circle &lt;/Address1> ##&lt;Address2> Bow Circle Business Ctr Unit 402 &lt;/Address2> ##&lt;City> Hilton Head Island&lt;/City> ##&lt;State> SC &lt;/State> ##&lt;Zip> 29926 &lt;/Zip> ##&lt;/AddUpdateLocation> ##&lt;/Transaction>'; ####$cable_command = $ARGV&#91;0&#93; #### or die "Could not find an argument \n"; print "$cable_command\n"; $socket_success_fl = 'N'; socket_connect(); if ($socket_success_fl =~ /Y/){ process_command(); socket_close(); }elsif ($socket_success_fl =~ /N/){ print "Unable to connect to socket!!....\n"; } sub socket_connect { socket (SH, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die $! +; my $dest = sockaddr_in (22928, inet_aton('172.17.15.102')); connect (SH, $dest) || die $!; $socket_success_fl = 'Y'; print "Connected to socket\n"; } sub process_command { my $v_param; my $buffer; $v_param = $cable_command; @command = split(/&/,$v_param); foreach $command (@command) { print "$command\n"; if ($command =~ /SVCPK=/ ) { $svcpk = substr($command,6,10); print "svcpk = ", $svcpk, " \n"; } else { print "About to send command\n"; send (SH, "$command\r", 0); print "sent command\n"; print "about to read response buffer\n"; sysread (SH, $buffer, 1000); print "read response buffer\n"; print "response = ", $buffer, " \n"; } } } sub socket_close{ close(SH); print "Socket closed \n";

    ++ to Perlbotics, who sent the above private message to show an old dog a new trick! Well Done!

    "Well done is better than well said." - Benjamin Franklin

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002592]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-07-12 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls