Recent

Author Topic: [Solved:Change to Synapse][Ask] Indy 10 XMLRPC client  (Read 17944 times)

Mujie

  • Jr. Member
  • **
  • Posts: 64
[Solved:Change to Synapse][Ask] Indy 10 XMLRPC client
« on: October 18, 2010, 02:46:10 pm »
Hi,
I'm trying to make a XML-RPC server with a simple XML-RPC client using IXR (The Incutio XML-RPC Library for PHP). A simple XML server is successfully to run, as well as a XML client.

Server with IXR :
Code: PHP  [Select][+][-]
  1. <?php
  2. //xmlrpc-server.php
  3. include('IXR_Library.php');
  4.  
  5. function addTwoNumbers($args) {
  6. $number1 = $args[0];
  7. $number2 = $args[1];
  8. return $number1 + $number2;
  9. }
  10.  
  11. $server = new IXR_Server(array('demo.addTwoNumbers' => 'addTwoNumbers'));
  12. ?>
  13.  

Client with IXR :
Code: PHP  [Select][+][-]
  1. <?php
  2. //xmlrpc-client-ixr.php
  3. include('IXR_Library.php');
  4. $client = new IXR_Client('hxxp://localhost/xmlrpc/xmlrpc-server.php');
  5. if (!$client->query('demo.addTwoNumbers', '100', '26')) {
  6.   die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
  7. }
  8.  
  9. echo '<pre>';
  10. print_r($client->getResponse());
  11. echo '</pre>';
  12. ?>
  13.  

Client without IXR :
Code: PHP  [Select][+][-]
  1. <?php
  2. //xmlrpc-client-nonixr.php
  3. $request = xmlrpc_encode_request('demo.addTwoNumbers', array('100', '26'));
  4. $context = stream_context_create(array('http' => array(
  5. 'method' => "POST",
  6. 'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
  7. 'content' => $request
  8. )));
  9.  
  10. $server = 'hxxp://localhost/xmlrpc/xmlrpc-server.php';
  11. $file = file_get_contents($server, false, $context);
  12. $response = xmlrpc_decode($file);
  13. echo '<pre>'.$response.'</pre>';
  14. ?>
  15.  

The result from client side is "126";

My question is, how use Indy 10 to build desktop client side as code describe above, parse it and show the result? I try with code below, but no luck :(

Code: Pascal  [Select][+][-]
  1. use .... IdHTTP, ....
  2. ....
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   lHTTP: TIdHTTP;
  6.   lParamList: TStringList;
  7.   Result: String;
  8.  
  9. begin
  10.   lHTTP := TIdHTTP.Create(nil);
  11.   lHTTP.Request.ContentType := 'text/xml';
  12.   lHTTP.Request.Accept := '*/*';
  13.   lHTTP.Request.Connection := 'Keep-Alive';
  14.   lHTTP.Request.Method := 'POST';
  15.   lParamList := TStringList.Create;
  16.   lParamList.Add('demo.addTwoNumbers');
  17.   lParamList.Add('100');
  18.   lParamList.Add('26');
  19.  
  20.   try
  21.     Result := lHTTP.Post('hxxp://localhost/xmlrpc/xmlrpc-server.php', lParamList);
  22.   finally
  23.     FreeAndNil(lHTTP);
  24.     FreeAndNil(lParamList);
  25.   end;
  26.  
  27.   Memo1.Text := Result;
  28.  
  29. end;
  30.  
« Last Edit: October 20, 2010, 02:31:30 am by Mujie »

Laksen

  • Hero Member
  • *****
  • Posts: 745
    • J-Software
Re: [Ask] Indy 10 XMLRPC client
« Reply #1 on: October 18, 2010, 05:18:52 pm »
Take a look at the specification, it's very simple: http://www.xmlrpc.com/spec

So essentially what you should send, should be something like
Code: Pascal  [Select][+][-]
  1.   lParamList.Add('<?xml version="1.0"?>');
  2.   lParamList.Add('<methodCall>');
  3.   lParamList.Add('   <methodName>demo.addTwoNumbers</methodName>');
  4.   lParamList.Add('   <params>');
  5.   lParamList.Add('      <param>');
  6.   lParamList.Add('         <value><i4>100</i4></value>');
  7.   lParamList.Add('         <value><i4>26</i4></value>');
  8.   lParamList.Add('      </param>');
  9.   lParamList.Add('   </params>');
  10.   lParamList.Add('</methodCall>');
  11.  

And the response(without headers) should look something like:
Code: [Select]
<?xml version="1.0"?>
<methodResponse>
   <params>
      <param>
         <value><i4>126</i4></value>
      </param>
   </params>
</methodResponse>

You can use TXMLDocument to parse that

Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: [Ask] Indy 10 XMLRPC client
« Reply #2 on: October 19, 2010, 12:24:52 am »
Take a look at the specification, it's very simple: http://www.xmlrpc.com/spec

So essentially what you should send, should be something like
Code: Pascal  [Select][+][-]
  1.   lParamList.Add('<?xml version="1.0"?>');
  2.   lParamList.Add('<methodCall>');
  3.   lParamList.Add('   <methodName>demo.addTwoNumbers</methodName>');
  4.   lParamList.Add('   <params>');
  5.   lParamList.Add('      <param>');
  6.   lParamList.Add('         <value><i4>100</i4></value>');
  7.   lParamList.Add('         <value><i4>26</i4></value>');
  8.   lParamList.Add('      </param>');
  9.   lParamList.Add('   </params>');
  10.   lParamList.Add('</methodCall>');
  11.  

Still no luck, I always got this :
Code: [Select]
<?xml version="1.0"?>
 <methodResponse>
  <fault>
    <value>
      <struct>
        <member>
          <name>faultCode</name>
          <value><int>-32700</int></value>
        </member>
        <member>
          <name>
          </name>
          <value><string>parse error. not well formed</string></value>
        </member>
      </struct>
    </value>
   </fault>
 </methodResponse>

Even I change <value><i4>100</i4></value> with <value><int>100</int></value>.
Anymore clue ?

Laksen

  • Hero Member
  • *****
  • Posts: 745
    • J-Software
Re: [Ask] Indy 10 XMLRPC client
« Reply #3 on: October 19, 2010, 08:06:26 am »
My mistake

Code: [Select]
  lParamList.Add('<?xml version="1.0"?>');
  lParamList.Add('<methodCall>');
  lParamList.Add('   <methodName>demo.addTwoNumbers</methodName>');
  lParamList.Add('   <params>');
  lParamList.Add('      <param>');
  lParamList.Add('         <value><i4>100</i4></value>');
  lParamList.Add('      </param>');
  lParamList.Add('      <param>');
  lParamList.Add('         <value><i4>26</i4></value>');
  lParamList.Add('      </param>');
  lParamList.Add('   </params>');
  lParamList.Add('</methodCall>');

Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: [Ask] Indy 10 XMLRPC client
« Reply #4 on: October 19, 2010, 08:50:32 am »
@Laksen : no, still not working, same error messages. Please take a look at http://stackoverflow.com/questions/301991, I follow their comment. Maybe Indy can't do this? Just guessing.

ik

  • Jr. Member
  • **
  • Posts: 88
  • ik
    • LINESIP
Re: [Ask] Indy 10 XMLRPC client
« Reply #5 on: October 19, 2010, 12:02:02 pm »
I've wrote a client using lNet and Lazarus in 2007, and still use it.
First I can open source it (it's not at the moment), but I tested it on on Linux 64 bit :)


Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: [Ask] Indy 10 XMLRPC client
« Reply #6 on: October 19, 2010, 12:10:57 pm »
@ik : Nevermind if you don't share your source, I don't bother it :) . But, if you don't mind, please break my problem at my thread above and give me a clue. Should I change Indy with another network component such a Synapse or LNet ? Many thanks for any hint and help.

ik

  • Jr. Member
  • **
  • Posts: 88
  • ik
    • LINESIP
Re: [Ask] Indy 10 XMLRPC client
« Reply #7 on: October 19, 2010, 12:37:54 pm »
A. I'm willing to GPL it, that's why I offered it :)
B. You might need the names of the parameters as well.

Try in the PHP also to print the parameters, you might be missing something there.
I use the IXR inside my own class so on the Init level I can also print_r the content (to log file).


Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: [Ask] Indy 10 XMLRPC client
« Reply #8 on: October 20, 2010, 12:28:52 am »
@ik : sound great :)

This day after struggle a few minutes, I found some weird char when I print out my XML client post with my past code.

Code: [Select]
Memo1.Lines.Text := lParamList.Text;

Code: [Select]
lParamList.Add('<?xml version="1.0" ?>');

The result is :
Code: [Select]
<?xml version="1.0"%20?%3E

OR

Code: [Select]
lParamList.Add('<?xml version="1.0"?>');

The result is :
Code: [Select]
<?xml version="1.0"?%3E

This is normal or was this caused the error?
« Last Edit: October 20, 2010, 12:49:19 am by Mujie »

Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: [Ask] Indy 10 XMLRPC client
« Reply #9 on: October 20, 2010, 02:30:35 am »
Thanks to @Dibo for his thread http://lazarus.freepascal.org/index.php/topic,10024.0.html. His question was solved my problem *ironic itsn't*. I change Indy with Synapse, and successfull.

Thanks to @ik and @Laksen too for their great help.

Code below, I hope this will be help another question :

Code: Pascal  [Select][+][-]
  1. uses ...HTTPSend, synautil...
  2. ....
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   aHTTP: THTTPSend;
  6. begin
  7.   aHTTP := THTTPSend.Create;
  8.   try
  9.     WriteStrToStream(aHTTP.Document, '<methodCall>'+
  10.                                      '<methodName>demo.addTwoNumbers</methodName>'+
  11.                                      '<params>'+
  12.                                      '<param><value><string>100</string></value></param>'+
  13.                                      '<param><value><string>26</string></value></param>'+
  14.                                      '</params>'+
  15.                                      '</methodCall>');
  16.     aHTTP.MimeType := 'application/xml';
  17.     if aHTTP.HTTPMethod('POST', 'hxxp://localhost/xmlrpc/xmlrpc-server.php') then
  18.     begin
  19.       aHTTP.Document.Position := 0;
  20.       Memo1.Lines.LoadFromStream(aHTTP.Document);
  21.     end else ShowMessage('Can not POST');
  22.   finally
  23.     aHTTP.Free;
  24.   end;
  25. end;
  26.  
« Last Edit: October 20, 2010, 02:40:12 am by Mujie »

rychu

  • Newbie
  • Posts: 1
Re: [Solved:Change to Synapse][Ask] Indy 10 XMLRPC client
« Reply #10 on: December 09, 2010, 12:20:39 am »
hi
Mujie, please try:
Code: [Select]
lHTTP.HTTPOptions:=[];

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Re: [Ask] Indy 10 XMLRPC client
« Reply #11 on: January 21, 2011, 01:15:21 pm »
Isn't this the simple matter of URL encoding on by Indy?

Isn't the server supposed to decode them, or is Indy not supposed to encode them before sending, or encoding it wrongly?

@ik : sound great :)

This day after struggle a few minutes, I found some weird char when I print out my XML client post with my past code.

Code: [Select]
Memo1.Lines.Text := lParamList.Text;

Code: [Select]
lParamList.Add('<?xml version="1.0" ?>');

The result is :
Code: [Select]
<?xml version="1.0"%20?%3E

OR

Code: [Select]
lParamList.Add('<?xml version="1.0"?>');

The result is :
Code: [Select]
<?xml version="1.0"?%3E

This is normal or was this caused the error?
Lazarus 3.0/FPC 3.2.2

 

TinyPortal © 2005-2018