XML-RPC instance of Apache XML-RPC

Source: Internet
Author: User
Tags tostring client
Apache|xml

Author: Seek ruler Build Source: Http://www.sentom.netXML-RPC is a remote procedure call protocol that works on the Internet. Popular point, is the use of HTTP protocol interaction, the interaction of the carrier is an XML file. XML-RPC Specific specifications please refer to here.
Photo from XML-RPC official website

The XML-RPC specification defines six data types, and the following table is the corresponding table of the six data types and Java data types.

Xml-rpcjava<i4> or <int>int<boolean>boolean<string>java.lang.string<double>double <datetime.iso8601>java.util.date<struct>java.util.hashtable<array>java.util.vector<base64 >byte[]

XML-RPC specification of various platforms are implemented concretely, XML-RPC specification Java implementation has several kinds, here we chose Apache XML-RPC.
The XML-RPC server implementation first defines a simple business object MyHandler, and the remote client invokes the object's method, as follows:


Package Net.sentom.xmlrpc;public class MyHandler {public string SayHello (String str) {return "Hello," + str;}}

Then define a servlet named Myxmlrpcserver, which the remote client accesses through Http-post.



Package Net.sentom.xmlrpc;import Java.io.ioexception;import Java.io.outputstream;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.apache.xmlrpc.xmlrpcserver;public class Myxmlrpcserver extends HttpServlet {public void DoPost (httpservletrequest Request, HttpServletResponse response) throws Servletexception, IOException {xmlrpcserver xmlrpc = new Xmlrpcserver (); Xmlrpc.addhandler ("MyHandler", New MyHandler ()); byte[] result = Xmlrpc.execute (Request.getinputstream ()); Response.setcontenttype ("Text/xml"); Response.setcontentlength (result.length); OutputStream out = Response.getoutputstream (); Out.write (result); Out.flush ();}}

Special notes are required:



Xmlrpc.addhandler ("MyHandler", New MyHandler ());

For ease of understanding, this can be seen as common:



MyHandler MyHandler = new MyHandler ();

Finally, add the following lines to the Web.xml file:



<servlet> <servlet-name>MyXmlRpcServer</servlet-name> <servlet-class> Net.sentom.xmlrpc.myxmlrpcserver</servlet-class></servlet><servlet-mapping> <servlet-name >MyXmlRpcServer</servlet-name> <url-pattern>/myxmlrpcserver</url-pattern></ Servlet-mapping>
XML-RPC Client implementation of the client is relatively simple, first to a Java client implementation myxmlrpcclient:


Package Net.sentom.xmlrpc;import Java.io.ioexception;import Java.net.malformedurlexception;import java.util.Vector ; Import Org.apache.xmlrpc.xmlrpcclient;import Org.apache.xmlrpc.xmlrpcexception;public class MyXmlRpcClient {public static void Main (string[] args) {try {xmlrpcclient xmlrpc = new Xmlrpcclient ("http://localhost:8080/XMLRPC/ Myxmlrpcserver "); Vector params = new vector ();p arams.addelement ("Tom"); String result = (string) xmlrpc.execute ("Myhandler.sayhello", params); SYSTEM.OUT.PRINTLN (result);} catch (Malformedurlexception e) {System.out.println (e.tostring ());} catch (Xmlrpcexception e) {System.out.println ( E.tostring ());} catch (IOException e) {e.printstacktrace ();}}

Http://localhost:8080/XMLRPC/MyXmlRpcServer is the Myxmlrpcserver access URL.



String result = (string) xmlrpc.execute ("Myhandler.sayhello", params);

One more Python client implementation



Import Xmlrpcliburl = ' http://localhost:8080/XMLRPC/MyXmlRpcServer '; server = Xmlrpclib. Server (URL);p rint server.myHandler.sayHello (' Tom ');


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.