折叠 编辑本段 XML-RPC
折叠 基本介绍
XML-RPC是工作在Internet上的远程过程调用协议。一个XML-RPC消息就是一个请求体为xml的http-po失呼士货细般当察严st请求,被调用的方法在服务器获头志型吧学端执行并将执行结果以xml格式编码后返回。
折叠 Requestexample
Here's an ex外打且反ample of an XML-RPC r药殖底随父equest:
POST /RPC2 HTT突含场P/1.0User-Agent: Frontier/5.1.2 (WinNT)Host: betty.userland.comContent-Typ叶赵同展e: text/xmlCont十尽尽ent-length: 181
折叠 Responseexample
Here's an example of a 况什验且适条止小支鱼response t夫采着油而o an XML-RPC request:
HTTP/1.1 200 OKConnection: closeContent程天器侵氢去女-Length: 158Content-Type: text/xmlDate: Fri, 17 Jul 1998 19:55:08 GMTServer: UserLand Frontier/5.1.2-WinNT
折叠 编辑本段 入门程序
折叠 伯民圆基本做法
以下的入门程言识叶件换似掉阳府序包括一个管理器(HelloHand江在粒ler)、一个服务器(HelloServer)、一个客户程序(HelloClient)。
首先要做的是创建用于远程过程调用的类和方法,人们常常称之为管理器。Xml-rpc管理器是一个方法和方法集,它接受xml-rpc请求,并对请求的内容进行解码,再向一个类些传则稳胡和方法发出请求。
折叠 管理器类
折叠 服务器类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 控属只23 24 25 26 且滑站斗况27 28 29 30 3保到航重陈短除基书1 32 | packagexm乙给溶新香省黄lRpc; /** * *<b><code>HelloServer</code></b>isasimpleXML-鸡RPCserver *thatwill鸡散肥备曲些叫切画弦takethe<c至争误异依内看力距丰史ode>HelloHandler</code>classavailable *forXML-PRCcalls. *<o:p */ importorg.apa手che.xmlrpc.WebServer; importorg.apache.xml设从压杂右门倒爱制生rpc.XmlRpc; importjava.IOException; publicclassHelloServer{ publicstaticvoidmain(String[]args){ if烈阿制(args.lengt宁h<1){ System.out.println("Usage:javaHelloServer[port]"); System.exit(-1); } try{ XmlRpc.持斯笔setDriver("org.apache.xerces.parsers.SAXParser"); //starttheserver System.out.println("StartingXML-RPCServer......"); WebServerserver=newWebServer(Integer.parseInt(args)吸针陈); //registerourhandlerclass server.addHandler("hello",newHelloHandler()); System.out.println("Nowacceptingrequ向车洲特ests......"); }catch(Cla浓去秋种尽胶增害ssNotFoundExceptione){ System.out.println("CouldnotlocateSAXDriver"); }catch(IOExceptione){ System.out.println("Couldnotstartserver:"+e.getMessage()); } } } |
折叠 客户程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 见蒸简35 36 37 38 39 40 | packagexmlRpc; /** * *<b><code>HelloClient</code></b>isasimpleXML-RPCclient *thatmakesanXML-RPCrequestto<code>HelloServer</code> */ importjava.i.IOException; importjava.util.Vector; importorg.apache.xmlrpc.XmlRpc; importorg.apache.x介真至况却mlrpc.XmlRpcClient; importjava简胞刑曲料济慢.t.MalformedURLException; importorg.apache.xmlrpc.XmlRpcException; publicclassHelloClient{ publicstaticvoidmain(String[]args){ if(args.length<1){ System.out.println("Usage:javaHelloClient[yourname]"); System.exit(-1); } try{ //UsetheApacheXerecesSAXDriver XmlRpc.setDriver("org.apache.xerces.parsers.SAXParser"); //Specifytheserv来都er XmlRpcClientclient=newXmlRpcClient("http://localhost:8585"); //createrequest Vectorparams=ne课配适wVector(); params.addElement(args); //makearequestandprinttheresult Stringresult=(String)client.execute("随缺谁hello.sayHello",params); System.out.println("Responsefr多延资查的止院宜派omserver:"+result); }c环调章省孔atch(ClassNotFoundExceptione){ System.out.相温拿兴当保刑println("Couldnotlo消她推草cateSAXDriver"); }catch(Malfo张rmedURLExceptione){ System.out.println("IncorrectURLfroxml-rpcserverfo加胡特当运西则司果述另ramt:"+e.getMessage()); }catch(XmlRpcExceptione){ System.out.绿哥切金沉杨造衡够println("XmlRpcException:"+e.getMessage()); }catc布米降金文全投树h(IOExceptione){ System.和喜木边配快企宗六映个out.println("IOException:"+e.getMessage()); } } } |
折叠 编辑本段 RP支房征题C和RMI
折叠 调用形式
味督卫水节止语害药在RMI和RPC之间最主要的区别在于方法是如何被调用的。在RMI中,远程接口使每个远程方法都具有方法签名。如果一个方法在服务器上执行,但是没有相匹配的签名被添加到这个远程接口上,那么这个新松研子石常都起振方法就不能被RMI客户方所调用。
折叠 classname.methodname的形式
折叠 methodname
这就向RPC服务器表明,被请求的方法在为"classname"的类中,名叫"methodname"。然后RPC服务器就去搜索与之相匹配的类和方法,并把它作为那种方法参数类型的输入。这里的参数类型是与RPC请求中的类型是匹配的。
折叠 匹配成功后
一旦匹配成功,这个方法就被调用了,其结果被编码后返回客户方。