XML-RPC Getting Started

xiaoxiao2021-03-06  126

First, what is XML-RPC

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. XML-RPC is a remote procedure call protocol working on the Internet. An XML-RPC message is a requestor XML HTTP-POST request, and the method being called executes and returns the execution result in XML format. REQUEST EXAMPLE Here's An Example of An XML-RPC Request: Post / RPC2 HTTP / 1.0User-agent: Frontier / 5.1.2 (WINNT) HOST: Betty.Userland.comContent-Type: Text / XMLContent-Length: 181

Examples.getStateName 41 Response example Here's an example of a response to 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

South Dakota

Second, XML-RPC Getting Started

The following entry includes a HelloHandler, a server (HelloClient). The first thing to do is to create classes and methods for remote processes calls, and people often call managers. XML-RPC Manager is a method and method set that accepts XML-RPC requests and decodes the request content, and a request is issued to a class and method. // Manager class package XMLRPC;

/ ** * @Author trier * * hellohandler is a simple handler tourler Than can * bereg * / public class hellohandler {public string self sayhello (String STRING Name) {return "Hello" Name;}} The server program registers the created manager to the server and specifies other specific parameters for the server. // server class package xmlrpc; / ** * * HelloServer is a simple xml-rpc server * That Will Take the HelloHandler class available * For xml-prc calls. * * / import org.apache.xmlrpc.Webserver; import org.apache.xmlrpc.xmlrpc; import java.io ioException;

Public class helloserver {public static void main (string [] args) {if (args.length <1) {system.out.println ("USAGE: Java HelloServer [port]); system.exit (-1);} Try {xmlrpc.setdriver ("org.apache.xerces.Parsrs.saxparser"); // Start the server system.out.println ("Starting XML-RPC Server ..."); WebServer Server = New Web Server (Integer.Parseint (Args [0])); // Register Our Handler Class Server.AddHandler ("Hello", New HelloHandler ()); System.out.Println ("Now Accepting Requests ...") Catch (ClassNotFoundException E) {System.out.println ("Could Not Locate Sax Driver);} Catch (IOException E) {System.out.Println (" Could Not Start Server: " E.getMessage ()) ;}}} // customer program package XMLRPC;

/ *** * HelloClient is a simple xml-rpc client * That makes an XML-RPC Request to HelloServer * / import java.io .IOException; import java.util.Vector; 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 main (string [] args) {if (args.length <1) {system.out.println ("USAGE: Java HelloClient [Your Name]); system.exit (-1); } try {// Use the Apache Xereces SAX Driver XmlRpc.setDriver ( "org.apache.xerces.parsers.SAXParser"); // Specify the server XmlRpcClient client = new XmlRpcClient ( "http: // localhost: 8585"); // create request vector params = new vector (); params.addelement (args [0]); // make a request and print the result string result = (string) Client.execute ("Hello.sayhello", params; System.out.println ( "Response from server:" result);} catch (ClassNotFoundException e) {System.out.println ( "Could not locate SAX Driver");} catch (MalformedURLException e) {System.out.println ("INCORRECT URL FRO XML-RPC Server Forumt: E.GetMessage ());} Catch (xmlrpcexception e) {system.out.println ("xmlrpcexce:" E.GetMessage ());} catch (ioException e) {system.out.println ("IOEXCEPTION:" E.getMessage ());}}}

Third, the simple comparison of RPC and RMI

The most important difference between RMI and RPC is how the method is not called. In RMI, the remote interface makes each remote method signature. If a method is executed on the server, but no matching signature is added to this remote interface, then this new method cannot be called by the RMI client. In the RPC, when a request arrives at the RPC server, this request contains a parameter set and a text value, usually forms the form of "classname.methodname". This indicates that the RPC server indicates that the method is called "MethodName" in the class of "classname". The RPC server then searches for classes and methods that match it, and use it as input to the method parameter type. The parameter type here is matched to the type in the RPC request. Once the match is successful, this method is called, and the result is encoded and returns to the client. Trier finishing: Reference: http://www.xmlrpc.com/ "Java & XML" O'Reilly

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

New Post(0)