2

我有一个简单的问题,我的 Java XMLRPC 客户端似乎无法正确地与用 TCL 编写的 XMLRPC 服务器对话

(使用 TCL XMLRPC SERVER OPEN SOURCE实现

摘要:TCL/Python 等中的 XMLRPC 客户端可以/确实向 TCL XMLRPC 服务器发送/接收消息,但我的 Java XMLRPC 客户端似乎无法正常工作。

Java客户端代码:

   /*
    * 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);

TCL 服务器对 Java 的 Debug ERROR 响应:

    //(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</value></param></params>
    bgerror failed to handle background error.
    Original error: 
    Error in bgerror: can't read "xmlcall": no such variable**

但是,Python 示例有效,还请注意我打印出 XML-debug 以查看成功的请求:

但是,如果我尝试使用 TCL 客户端,甚至是简单的 Python XMLRPC 客户端,它就可以工作。我什至使用 Python 打印出 XMLRPC 请求:

(来自 Python 客户端,没什么花哨的,)

    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<?xml version='1.0'?>\n<methodCall>\n<methodName>hello</methodName>\n<params>\n<param>\n<value><string>hello world</string></value>\n</param>\n</params>\n</methodCall>\n"

    reply: 'HTTP/1.1 200 OK\n'
    header: Content-Type: text/xml
    header: Content-length: 162
    body: '<?xml version="1.0"?>\n<methodResponse>\n\t<params>\n\t\t<param>\n\t\t\t<value>     <string>hello(hello world) yaaaah?!</string></value>\n\t\t</param>\n\t</params>\n</methodResponse>\n'

在推回正确响应之前,TCL 服务器对 Python 的调试/响应:

    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

这是 hello( arg ) 的 TCL XMLRPC 服务器代码,适用于 tcl、python,而不是 java。(可能是java客户端配置问题)

    #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

我很感激任何指示,到目前为止,我已经尝试使用带有嵌入式 TCL 解释器的 Java 或 Python 来避免这种情况,但是由于该应用程序必须使用、获取和共享大量 TCL,我必须得到一个 TCL XMLRPC 服务器启动并工作。

我还尝试将 webservices httpd 与 XMLRPC 一起使用,但即使让它与 tcl/python 客户端一起使用也没有太多成功。

已经杀死了整个周末。

感谢您的阅读,以及任何指针/帮助。


找到解决方案(在此处发布)

  1. 问题归结为旧 tcl xmlrpc 服务器的 XML 不包括数据类型。它们是隐含的,因此为了让 apache 的 XMLRPC 客户端围绕字符串发送隐含的数据类型,只需实现“自定义数据类型”以放回标签。

代码在这里:

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 <string> 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);
            }
        }
    }
}
4

1 回答 1

0

问题归结为旧 tcl XMLRPC 服务器的 XML 不包括数据类型。它们是隐含的,因此为了让 Apache 的 XMLRPC 客户端围绕字符串发送隐含的数据类型,只需实现“自定义数据类型”以放回标签。

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 <string> 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);
            }
        }
    }
}
于 2017-03-04T23:27:21.990 回答