[Tutorial] XML-RPC Web Service
Author |
Message |
rizzix
|
Posted: Sun Oct 05, 2003 1:45 pm Post subject: [Tutorial] XML-RPC Web Service |
|
|
What is a web service? Well lets just say it's an emerging new technology. For more information take a look at these articles at: http://webservices.org/index.php/article/articleview/429/1/61/
I'm going to take you through a quick tutorial on a RPC type web service, that runs on the XML-RPC standards. The libraries commonly used for xml-rpc in java is the one provided by Apache.org. You can get more information on the library at their website. But i've attached the latest version as of today (Oct 5, 2003).
The next question is what is XML?
Its a markup language just like HTML. But a lot more versatile. For more information on XML do take a look at w3c website. I won't be saying much on this, since you don't really need to know XML to create an xml-rpc web service. Of course knowledge on xml is helpful.
The final question: what is RPC?
RPC stands for Remote Procedure Call. Think of it as your application calling a procedure of another application that is located remotely. This is exciting stuff. Since the RPC works by sending XML data instead of bits and bytes, it is highly interoperable, i.e any type of application can call any applications procedures etc. This means that if you write a server that hosts some methods for use in, lets say Perl, a PHP, Java, C/C++, any type of app can call those methods hosted.
So lets start...
The Server:
Java: |
//
// TestServer.java
//
import org.apache.xmlrpc.WebServer;
public class TestServer {
public String message () {
return "It Works! Yahooooo!";
}
public static void main (String[] args ) {
try {
WebServer server = new WebServer (8090);
server. addHandler("test", new TestServer ());
server. start();
} catch (Exception e ) {
System. out. println(e. getMessage());
}
}
}
|
This is a simple server that uses the http protocol, thus a webserver.
We create a server object and pass it the port to listen to. Then we register an object with the server with a name. and finally we start the server. In this case we registered the test server object with the server with the name test.
The Client:
Java: |
//
// TestClient.java
//
import org.apache.xmlrpc.XmlRpcClient;
import java.util.Vector;
public class TestClient {
public static void main (String[] args ) {
try {
XmlRpcClient server = new XmlRpcClient ("127.0.0.1", 8090);
String msg = (String) server. execute("test.message", new Vector());
System. out. println("message recieved: " + msg );
System. out. println("from: " + server. getURL());
} catch (Exception e ) {
System. out. println(e. getMessage());
}
}
}
|
Here we create an XmlRpcClient object. The client object represents a connection to the server. We call the execute method passing the method to call, and a Vector of arguments to pass to the method. If there are not arguments to pass, send in an empty vector object. The execute method has a return type of Object, so you will have to cast the object to String after all the message method of the TestServer object (which was registered as 'test') returns a String object. Then the rest is quite obvious.
Try it out...
before compilation make sure the two xml-rpc jar files are in the same directory as the java files you've written.
to compile open the command prompt and type this in:
code: | javac -classpath xmlrpc-1.2-b1.jar Test*.java |
To run the Server type this in:
code: | java -classpath xmlrpc-1.2-b1.jar;. TestServer |
Open another command prompt and type this in to run the Client:
code: | java -classpath xmlrpc-1.2-b1.jar;. TestClient |
and watch!
Description: |
|
Download |
Filename: |
xmlrpc-1.2-b1.zip |
Filesize: |
333.96 KB |
Downloaded: |
1858 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
nasr25
|
Posted: Wed Jan 23, 2013 5:22 am Post subject: RE:[Tutorial] XML-RPC Web Service |
|
|
very thank for that
i was search abouy xml rpc from a month
thanks and have a nice work
|
|
|
|
|
|
nasr25
|
Posted: Wed Jan 23, 2013 5:28 am Post subject: RE:[Tutorial] XML-RPC Web Service |
|
|
very thank for that
i was search abouy xml rpc from a month
thanks and have a nice work
|
|
|
|
|
|
|
|