Using XML-RPC to access Web services

Source: Internet
Author: User
Tags format web services client
Web|web Service |xml| Access <!--[if!supportlists]-->1.    <!--[endif]-->xml-rpc<!--[if!supportlists]-->a)          <!--[endif]--> principle: XML-RPC is the way to send RPC calls, using HTTP as a transport protocol, to encapsulate the requests and requests that need to be sent to the HTTP message. Because the RPC call is also a method call, only the party being invoked is not local, although this is true, but what cannot be changed is the nature of the call, for the caller, he needs to provide the parameters of the method and method being invoked, and for the callee, she performs the invocation of the response by interpreting the caller's request, and returns the result. This is the nature of the method invocation. RPC only increases the network transmission and decoding portion between calls and returns, while XML-RPC provides that encoding and decoding must be in XML format, while network traffic must use the HTTP protocol.  <!--[If!supportlists]-->2.    <!--[ENDIF]--&GT;XML-RPC request and Response format <!--[if!supportlists]-->a)          <!--[endif]--> Request: Post/rpchandler http/1.0user-agent:acmexmlrpc/1.0host: Xmlrpc.example.comcontent-type:text/xmlcontent-length:165<?xml version= "1.0"?><methodcall>      <methodName>getCapitalCity</methodName>     <params>          &LT;PARAM&GT;&NBSP;&NBSP;&NBSP;&NBsp;          <value><string>England</string>< /value>         </param>     </params ></methodCall> <!--[if!supportlists]-->b)         <!-- [endif]--> response: http/1.1 Okdate:sun, APR 2001 12:08:58 gmtserver:apache/1.3.12 (Unix) Debian/gnu PHP/4.0.2Connect Ion:closecontent-type:text/xmlcontent-length:133<?xml version= "1.0"?><methodresponse>      <params>         <param>               <value><string>Michigan</string> </value>         </param>     </ params></methodresponse> <!--[if!supportlists]-->c)       &nbsp <!--[endif]--> Error: http/1.1 Okdate:sun, APR 2001 12:08:58 gmtserver:apache/1.3.12 (Unix) Debian/gnu PHP/4.0.2 Connection:closecontent-type:text/xmlcontent-length:133<?xml version= "1.0"?><methodresponse>      <fault>         <value>               <struct>                    <member>                        <name>faultCode</name>                        <value><int>802</int></value >                   </member>                    <member>                        <name>faultString</name>                         <value><string>unknown country, ' Engand ' .</string></value>                    </member >              </struct>          </value>     </fault></methodresponse > <!--[If!supportlists]-->3.    <!--[Endif]-->xml-rpc instance <!--[if!supportlists]-->a) &NBSP;&Nbsp;      <!--[endif]--> Send request:<!--[if!supportlists]-->                       i.               <!--[endif]--> Note: Use Xmlrpcclient to send a request, in which we need to provide the name of the method to call, as well as the method parameters, and the parameters need to be wrapped in a collection. <!--[If!supportlists]-->                    ii.               <!--[endif]--> code: Xmlrpcclient client = new Xmlrpcclient ("http://127.0.0.1:6666"); Vector<string> pars = new vector<string> ();p ars.addelement ("Nick"); Object result = Client.execute (" Service.hello ", pars); SYSTEM.OUT.PRINTLN ("The result is:" + result); <!--[if!supportlists]-->b)          <!--[endif]--> Publishing Service:<!--[if!supportlists]-->                       i.              <!--[endif]--> Note: Through the webserver we can very convenient publishing services. The instance that is added to AddHandler () is the service body, and "service" represents the service name. When the request arrives, he will find the service body based on the requested name and then invoke the Execute () method on the returned instance. <!--[If!supportlists]-->                    ii.               <!--[endif]--> code: WebServer Server = new WebServer (6666); Server.addhandler ("Service", new Service ()); Server.start ();  public class Service implements Xmlrpchandler {     public Object Execute ( String methodname, Vector args) throws Exception {         if ("Service.hello" . Equals (methodname)) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;    return Hello ((String) args.get (0));        }          return "";    }     Private string Hello (string name) {         return "Hello" + name;   & nbsp }}


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.