XML-RPC Apache XML-RPC instance

xiaoxiao2021-03-06  55

PRE.ProgramListing {font-family: Courier, Lucida Console

FONT-SIZE: 100%;

Margin-left: 0px;

Margin-Right: 0px;

Margin-top: 5px;

Margin-bottom: 5px;

Border: # 164781 1px solid;

Padding: 10px;

COLOR: # 003366;

Background: # C2d1e7 none}

Author: En construction Source: http: //www.sentom.net

XML-RPC is a remote procedure call protocol working on the Internet. Popular point, that is, interact with the HTTP protocol, the carrier interacting is an XML file. XML-RPC specific specification, please refer to

Here.

Image from XML-RPC official website

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

XML-RPCJAVA or int Boolean java.lang.string double java.util.date java.util.hashtable java. Util.Vector byte []

The various platforms of the XML-RPC specification have specific implementations, and the XML-RPC-specific Java implementation has several, here we have selected Apache XML-RPC.

XML-RPC server implementation first defines a simple business object myHandler, the remote client will call the object's method, the specific code is as follows:

Package net.sentom.xmlrpc;

Public class myhandler {

Public String Sayhello (String Str) {

Return "Hello," Str;

}

}

Then define a servlet called MyXMLRPCSERVER, and the remote client accesses the servlet via 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 instructions are:

XMLRPC.AddHandler ("MyHandler", new myHandler ());

In order to understand, it can be seen as a normal:

MyHandler myhandler = new myhandler ();

Finally, add the following lines in the web.xml file:

myxmlrpcserver

net.sentom.xmlrpc.myxmlrpcserver

myxmlrpcserver

/ myxmlrpcserver

The XML-RPC client implements a relatively simple client, first come a Java client to implement MyXMLRPCCLIENT:

Package net.sentom.xmlrpc;

Import java.io.ioException;

Import java.net.malformedurlexception;

Import java.util.vector;

Import org.apache.xmlrpc.xmlrpclient;

Import Org.Apache.xmlrpc.xmlrpcexception;

Public class myxmlrpclient {

Public static void main (String [] args) {

Try {

XMLRPCCCLIENT XMLRPC = New XMLRPCCCLIENT ("http: // localhost: 8080 / xmlrpc / myxmlrpcserver");

Vector params = new vector ();

Params.AddeElement ("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 Access URL for MyXMLRPCServer.

String result = (string) Xmlrpc.execute ("myhandler.sayhello", params);

Come back a Python client.

Import XMLRPClib

URL = 'http: // localhost: 8080 / xmlrpc / myxmlrpcserver'; Server = XmlrpClib.server (URL);

Print Server.myHndler.SAYHELLO ('Tom');