title: Java XmlRpc Client POST issue with TCL XMLRPC Server
body: <p>I am having a simple problem, my Java XMLRPC Client cant seem to properly speak to the XMLRPC Server that is written in TCL</p>
  
  <p>(Using TCL XMLRPC SERVER OPEN SOURCE <a href="http://sourceforge.net/projects/xmlrpctcl/" rel="nofollow">implementation</a>)</p>
  
  <p>Summary:  XMLRPC Clients in TCL/Python etc, can/do send/receive messages to the TCL XMLRPC Server, but my Java XMLRPC client doenst seem to work.</p>
  
  <p><strong>Java Client Side Code:</strong></p>
  
  <pre><code>   /*
      * try's, catches, comments removed to show code-flow w/ out mess.
      * host/port/target all same as whats set in Python
      */
      //show imports / package used, this is using apache's xmlrpc v3.1.3
      import org.apache.xmlrpc.XmlRpcException;
      import org.apache.xmlrpc.client.XmlRpcClient;
      import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
      //... host and port are the same as whats used in working tcl/python clients. (remoteHostName / 5555)
      //... method is the same as well, 'fooBar123', and args is just 1 string passed to it.
      XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
      target = "RPC2";
      String targetUrl = "http://"+host+":"+port+"/" + target;
      TestNgUtil.ReportInfo("config.SetServerUrl("+targetUrl+")");
      config.setServerURL(new URL(targetUrl));
      XmlRpcClient client = new XmlRpcClient();
      client.setConfig(config);
      String result = null;
      /*
      * This Result Never Returns from TCL XMLRPC Server
      */
      result = (String) client.execute(command, params);
  </code></pre>
  
  <p><strong>TCL Server's Debug ERROR Response to Java:</strong></p>
  
  <pre><code>    //(notice unlike Python example below, no proper Header, Content-Type, etc)
  
      TCL Server Side of the Java Error
      in serveOnce: addr: 10.21.69.13
      in serveOnce: port: 64522
      Unknown type: fooBar123&lt;/value&gt;&lt;/param&gt;&lt;/params&gt;
      bgerror failed to handle background error.
      Original error: 
      Error in bgerror: can't read "xmlcall": no such variable**
  </code></pre>
  
  <p>Python example works, however, also note I print out the XML-debug to look at successful requests:</p>
  
  <p>However, If I attempt to use the TCL client, or even a simple Python XMLRPC client, it works.
  I even use Python to print out the XMLRPC request:</p>
  
  <p>(from Python client, <a href="http://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-python.html" rel="nofollow">nothing</a> fancy,  )</p>
  
  <pre><code>    import xmlrpclib
      server_url = "http://remoteHostName:5555";
      server = xmlrpclib.Server(server_url, verbose=True);
      result = server.hello('hello world')
      ## DEBUG INFO PRINTED FROM REQUEST POST ##
      send: "POST /RPC2 HTTP/1.1\r\nHost: remoteHostName:5555\r\nAccept-Encoding: gzip\r\nUser-Agent: xmlrpclib.py/1.0.1 (by www.pythonware.com)\r\nContent-Type: text/xml\r\nContent-Length: 160\r\n\r\n&lt;?xml version='1.0'?&gt;\n&lt;methodCall&gt;\n&lt;methodName&gt;hello&lt;/methodName&gt;\n&lt;params&gt;\n&lt;param&gt;\n&lt;value&gt;&lt;string&gt;hello world&lt;/string&gt;&lt;/value&gt;\n&lt;/param&gt;\n&lt;/params&gt;\n&lt;/methodCall&gt;\n"
  
      reply: 'HTTP/1.1 200 OK\n'
      header: Content-Type: text/xml
      header: Content-length: 162
      body: '&lt;?xml version="1.0"?&gt;\n&lt;methodResponse&gt;\n\t&lt;params&gt;\n\t\t&lt;param&gt;\n\t\t\t&lt;value&gt;     &lt;string&gt;hello(hello world) yaaaah?!&lt;/string&gt;&lt;/value&gt;\n\t\t&lt;/param&gt;\n\t&lt;/params&gt;\n&lt;/methodResponse&gt;\n'
  </code></pre>
  
  <p><strong>TCL Server's  Debug/Response to Python, before pushing back proper response:</strong></p>
  
  <pre><code>    send: "POST /RPC2 HTTP/1.1
      Host: remoteHostName:5555
      Accept-Encoding: gzip
      User-Agent: xmlrpclib.py/1.0.1 (by www.pythonware.com)
      Content-Type: text/xml
      Content-Length: 156
  </code></pre>
  
  <h2>here's the TCL XMLRPC Server code for hello( arg ), works for tcl, python, not java. (java client configuration issue probably)</h2>
  
  <pre><code>    #using the TCL XMLRPC Server ( http://sourceforge.net/projects/xmlrpctcl/ ) 
      package require xmlrpc
      xmlrpc::serv 5555
  
      proc hello { world } {
    puts "IN HELLO WORLD!"
    set res "hello(${world}) yaaaah?!"
    return [list string $res]
      }
  
      vwait forever
  </code></pre>
  
  <p>I appreciate any pointers, I've gone so far to attempt to use Java or Python with embedded TCL interpretters to avoid this, but because of the large amount of TCL that this application has to use, source, and share, I have to get a TCL XMLRPC server up and working.</p>
  
  <p>I've also attempted to use the webservices httpd with XMLRPC, but did not have much sucess even with getting it to work with tcl/python clients.</p>
  
  <p>Killed a whole weekend on this already.</p>
  
  <p>Thanks for reading, and any pointers/help.</p>
  
  <hr>
  
  <p>SOLUTION FOUND (POSTED HERE)</p>
  
  <ol>
  <li>the problem boils down to the XML of the old tcl xmlrpc server not including  on the data types.  They're implied, so in order to get apache's XMLRPC client to send the implied  data types around strings, just implement the 'custom data type' to put the tag back.</li>
  </ol>
  
  <p>CODE IS HERE:</p>
  
  <pre><code>import java.net.URL;
  
  import org.apache.xmlrpc.client.XmlRpcClient;
  import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  import org.apache.xmlrpc.common.TypeFactoryImpl;
  import org.apache.xmlrpc.common.XmlRpcController;
  import org.apache.xmlrpc.common.XmlRpcStreamConfig;
  import org.apache.xmlrpc.serializer.StringSerializer;
  import org.apache.xmlrpc.serializer.TypeSerializer;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  
  public class XMLRPCClient {
      public static void main(String[] argv) throws Exception {
          XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
          config.setServerURL(new URL("http://127.0.0.1:6800/rpc"));
          XmlRpcClient client = new XmlRpcClient();
          client.setConfig(config);
          client.setTypeFactory(new MyTypeFactoryImpl(client));
          Object[] params = new Object[] {
              new String[] { "http://www.google.com" }
          };
          String result = (String)client.execute("aria2.addUri", params);
      }
  
      static private class MyStringSerializer extends StringSerializer {
          public void write(ContentHandler pHandler, Object pObject)
              throws SAXException {
              // Write &lt;string&gt; tag explicitly
              write(pHandler, STRING_TAG, pObject.toString());
          }
      }
  
      static private class MyTypeFactoryImpl extends TypeFactoryImpl {
          public MyTypeFactoryImpl(XmlRpcController pController) {
              super(pController);
          }
  
          public TypeSerializer getSerializer(XmlRpcStreamConfig pConfig, Object pObject) throws SAXException {
              if(pObject instanceof String) {
                  return new MyStringSerializer();
              } else {
                  return super.getSerializer(pConfig, pObject);
              }
          }
      }
  }
  </code></pre>