【发布时间】:2012-11-18 21:21:40
【问题描述】:
我有一个简单的问题,我的 Java XMLRPC 客户端似乎无法正确地与用 TCL 编写的 XMLRPC 服务器对话
(使用TCL XMLRPC SERVER 开源implementation)
总结: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 客户端,nothingfancy,)
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 客户端一起使用也没有太多成功。
已经为此浪费了整个周末。
感谢您的阅读,以及任何指示/帮助。
找到的解决方案(在此处发布)
- 问题归结为旧 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);
}
}
}
}
【问题讨论】:
-
sourceforge.net/apps/phpbb/aria2/… 已解决。添加自定义序列化程序以启用它。旧的 xmlrpc tcl 服务器不符合 imlied
标签。 -
如果你已经解决了,写下所需的答案。像这样,其他用户(和搜索引擎)更容易找到您的答案并帮助他们。这就是 Stack Overflow 的工作原理。
-
在问题结束时更新,我无法将其标记为解决方案,因为我解决了自己的问题。
-
离开我的帐户有一段时间了,将其作为解决方案正确发布。