Apache XML-RPC Getting Started

zhaozj2021-02-11  164

The method used by Apache XML-RPC is used because of the recent project needs.

XML-RPC is a set of specifications and a series of implementations that allow procedures for running in different operating systems, different environments. This remote process call uses HTTP as the transport protocol, XML as the encoding format of the transfer information. The definition of XML-RPC is as simple as possible, but it can be transmitted, processed, and returns complex data structures. For more detailed information about XML-RPC, see http://www.xmlrpc.com.

1, customer program Apache XML-RPC provides two customer classes: org.apache.xmlrpc.xmlrpclient: Using java.net.urlConnection. ORG.APache.Xmlrpc.xmlrpcclientLite: It provides a lightweight HTTP Client implementation. If you need a full HTTP support (for example: proxy, redirect, etc.), you should use XMLRPCCCLIENT. Conversely, if you don't need full HTTP support and pay more attention to performance, then you should carefully test these two customer classes. On some platforms, xmlrpcclient may be faster, but XMLRPCCCLIENTLITE is faster on some platforms. These two customer classes provide the same interface.

It is very simple to use Apache XML-RPC in the client, just complete the following simple work: // Establish XML-RPC client xmlrpcclient client = new xmlrpcclient ("http: //" Server ": port);

// Set call parameters Vector params = new vector (); params.addelement (name);

// Call and acquire String Result = (String) Client.execute ("Hello.sayhello", params);

If you need to perform asynchronous calls and use the executeasync () method.

2, register Handler Object Org.apache.xmlrpc.xmlrpcserver and org.apache.xmlrpc.Webserver provides methods to register / log out of Handler Object: addHandler (String name); RemoveHandler (String name);

3, using XML-RPC typical code in the servlet environment as follows: Xmlrpcserver Xmlrpc = new xmlrpcserver (); xmlrpc.addhandler ("examples", new exampleHandler ()); ... Byte [] Result = Xmlrpc.execute (request.getInputStream ()); response.setContentType ( "text / xml"); response.setContentLength (result.length ()); OutputStream out = response.getOutputStream (); out.write (result); out.flush ( ); Please note that the execute method does not return any exceptions because all errors are encoded into XML returns to end. 4. Use the built-in HTTP Server code as follows: Xmlrpc.SetDriver ("org.apache.xerces.parsers.saxparser");

// Start the server system.out.println ("Starting XML-RPC Server ...);

Webserver Server = New Web Server (8585); // Register Our Handler Class Server.AddHandler ("Hello", New HelloHandler ()); Server.Start ();

5. Apache XML-RPC support type These types apply to the parameters of XML-RPC and return types, and if the parameter or return type is a collection type, it is also suitable for collection elements.

XML-RPC data typeData Types generated by the ParserTypes expected by the Invoker as input parameters of RPC handlers or java.lang.Integerint java.lang.Booleanboolean java.lang.Stringjava.lang .String java.lang.Doubledouble java.util.Datejava.util.Date java.util.Hashtablejava.util.Hashtable java.util.Vectorjava.util.Vector Byte [] byte []

6. Simple example using built-in HTTP Server

a, establish a handler object

/ * * Create Date 2004-5-12 * * Change the generated file template to * Window> Preferences> Java> Code Generation> Code and Note * / package helloxmlrpc; import java.util.Vector; / ** * @author Fyun * * Change the template for the generated type annotation to * Window> Preferences> Java> Code Generation> Code and Note * / Public Class HelloHandler {Public STRING SAYHELLO (String Name) {Return "Hello" Name;}}

B, register and start server / * * Create Date 2004-5-12 * * Change the generated file template to * Window> Preferences> Java> Code Generation> Code and Note * / package helloxmlrpc; / ** * @Author fyun * * Change the template for the generated type annotation to * Window> Preferences> Java> Code Generation> Code and Note * / Import Org.Apache.xmlrpc. *; Public Class HelloServer {PUBLIC VOID INITSERVER () {Try {XMLRPC. SetDriver ("org.apache.xerces.Parsrs.saxparser"); // Start the server system.out.println ("Starting XML-RPC Server ..."); WebServer Server = New Web Server (8585); // Register Our Handler Class Server.AddHandler ("Hello", New HelloHandler ()); Server.Start (); System.Out.println ("Now Accepting Requests ...");} catch (ClassNotFoundException E ) {System.out.println ("COULD NOT Locate sax driver ");}} public static void main (String [] args) {INITSERVER ();}} C, customer program / * * Create Date 2004-5-12 * * Change The generated file template is * Window> Preferences> Java> Code Generation> Code and Note * / Package Helloxmlrpc; / ** * @AuThor Fyun * * Template for Generating Type Note To * Window> Preferences> Java> Code Generation> Code and Comment * / Import Java.io.ioException; import org.apache.xmlrpc.xmlrpc; import org.apache.xmlrpc.xmlrpcclient; import java.net.mAlFormedurlexception;

import org.apache.xmlrpc.XmlRpcException; public class HelloClient {public static void invoke (String server, String port, String name) {try {// Use the Apache Xereces SAX Driver XmlRpc.setDriver ( "org.apache.xerces.parsers . Saxparser "); // specify the server xmlrpcclient client = new xmlrpcclient (" http: // " server ": " port); // create request vector params = new vector (); params.addelement (name) ; // make a request and print the result String result = (String) client.execute ( "hello.sayHello", params); System.out.println ( "hello.sayHello:" result);} catch (ClassNotFoundException e ) {System.out.println ("could not locate sax driver);} catch (mALFORMEDURLEXCEPTION E) {system.out.println (" IncorRect URL FRO XML-RPC Server F ORAMT: " E.GetMessage ());} catch (xmlrpcexception e) {E.PrintStackTrace (); system.out.println (" xmlrpcexce: " E.getMessage ());} catch (oioException e) {system .out.println ("ioException:" E.getMessage ());} catch (exception e) {E.PrintStackTrace ();}} public static void main (String [] args) {if (args == null | | args.length <2) {system.out.println ("USAGE: Java HelloClient [Server] [Port] [YourName]"); System.exit (1);

} Invoke (args [0], args [1], args [2]);}} 7, using servlet example 1, handler object does not change 2, establish XMLRPCFACADE PACKAGE Helloxmlrpc; import javax.servlet.http.httpservletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import org.apache.xmlrpc.XmlRpcServer; public class XmlRpcFacade {private static XmlRpcServer xmlrpc; static {xmlrpc = new XmlRpcServer (); / / register your handler object xmlrpc.addHandler ( "hello", new HelloHandler ());} public void execute (HttpServletRequest request, HttpServletResponse response) throws IOException {byte [] result = xmlrpc.execute (request.getInputStream ()); Response.setContentType ("Text / XML; Charset = GB2312"); response.setContentLength; OutputStream O; OutputStream O; OutputStream O Ut = response.getOutputStream (); out.write (result); out.flush (); out.close ();}} 3, establish servlet package hellpxmlrpc; import javax.servlet. *; import javax.servlet.http. *; import java.io. *; import java.util *;. public class XmlRpcServlet extends HttpServlet {private static final String CONTENT_TYPE = "text / html; charset = GBK"; private XmlRpcFacade facade; public void init () throws ServletException { Facade = new xmlrpcfacade ();

转载请注明原文地址:https://www.9cbs.com/read-5675.html

New Post(0)