in reply to XMLRPC - doing it the hard way
Update: changed the client code and results to use the correct port (reversing a change I made while testing).
In your first client, you need to flush your output, otherwise nothing is sent to the server, and, as jethro said, you need two linefeeds between the headers and body of your request. You can try the following:
#!/usr/bin/perl use strict; use Socket; use IO::Handle; # initialize host and port my $host = 'localhost'; my $port = 8999; # my $port = 80; my $proto = getprotobyname('tcp'); # get the port address my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); # create the socket, connect to the port socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET, $paddr) or die "connect: $!"; SOCKET->autoflush(1); # create data my $data = <<"START" ; <?xml version="1.0"?> <methodCall> <methodName>sum</methodName> <params> <param> <value><double>1.12</double></value> </param> <param> <value><double>4.12</double></value> </param> </params> </methodCall> START my $header = <<"START" ; POST /quotes.cgi HTTP/1.0 Host: localhost Content-Type: text/xml START $header .= "Content-length: " . length($data) ; my $content = $header. "\n\n" . $data ; print $content ; print SOCKET "$content\n" or die "print SOCKET: $!"; print "waiting for response\n"; my $line; while ($line = <SOCKET> ) { print $line; } close SOCKET or die "close: $!";
On my system, this produces:
POST /quotes.cgi HTTP/1.0 Host: localhost Content-Type: text/xml Content-length: 209 <?xml version="1.0"?> <methodCall> <methodName>sum</methodName> <params> <param> <value><double>1.12</double></value> </param> <param> <value><double>4.12</double></value> </param> </params> </methodCall> waiting for response HTTP/1.1 403 Forbidden Date: Mon, 15 Dec 2008 16:09:28 GMT Server: libwww-perl-daemon/5.818 Content-Type: text/html Content-Length: 53 <title>403 Forbidden</title> <h1>403 Forbidden</h1>
I am not familiar with the Frontier XML RPC2 protocol, but looking at the server it appears your request must use the path /RPC2. From Frontier/Daemon.pm:
while ($conn = $self->accept) { my $rq = $conn->get_request; if ($rq) { if ($rq->method eq 'POST' && $rq->url->path eq '/RPC2') { ${*$self}{'response'}->content(${*$self}{'decode'}->se +rve($rq->content, ${*$self}{'methods'})); $conn->send_response(${*$self}{'response'}); } else { print STDERR "got an invalid request\n"; $conn->send_error(RC_FORBIDDEN); } } $conn->close; $conn = undef; # close connection }
Update: Change the path in your request to /RPC2 and it appears to work:
#!/usr/bin/perl use strict; use Socket; use IO::Handle; # initialize host and port my $host = 'localhost'; my $port = 8999; # my $port = 80; my $proto = getprotobyname('tcp'); # get the port address my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); # create the socket, connect to the port socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET, $paddr) or die "connect: $!"; SOCKET->autoflush(1); # create data my $data = <<"START" ; <?xml version="1.0"?> <methodCall> <methodName>sum</methodName> <params> <param> <value><double>1.12</double></value> </param> <param> <value><double>4.12</double></value> </param> </params> </methodCall> START my $header = <<"START" ; POST /RPC2 HTTP/1.0 Host: localhost Content-Type: text/xml START $header .= "Content-length: " . length($data) ; my $content = $header. "\n\n" . $data ; print $content ; print SOCKET "$content\n" or die "print SOCKET: $!"; print "waiting for response\n"; my $line; while ($line = <SOCKET> ) { print $line; } close SOCKET or die "close: $!";
produces
POST /RPC2 HTTP/1.0 Host: localhost Content-Type: text/xml Content-length: 209 <?xml version="1.0"?> <methodCall> <methodName>sum</methodName> <params> <param> <value><double>1.12</double></value> </param> <param> <value><double>4.12</double></value> </param> </params> </methodCall> waiting for response HTTP/1.1 200 OK Date: Mon, 15 Dec 2008 16:17:46 GMT Server: libwww-perl-daemon/5.818 Content-Length: 128 Content-Type: text/xml <?xml version="1.0"?> <methodResponse> <params> <param><value><double>5.24</double></value></param> </params> </methodResponse>
|
|---|