Client calls Xfire WebService (with JavaScript)

Source: Internet
Author: User
Tags soap wsdl

I. Creating clients through interfaces provided by the Web service side

/** * Create clients through the interface provided by the Web service side * @see client must provide an interface that is fully consistent with the server, the package name is also consistent * @see and need to introduce Xfire 1.2 Core libraries and Xfire 1.2 HTTP in the project at this time Client Libraries */public class Portclient {public static void main (String args []) {//First use Xfire objectservicefactory from H The Elloservice interface creates a service model ServiceModel//servicemodel contains a description of the service, in other words, the service's metadata service ServiceModel = new ObjectS              Ervicefactory (). Create (Kingteamservice.class);        The address of the access String serviceurl = "Http://localhost:80/xfireD/services/KingTeamService";          Obtain an agent factory object for Xfire xfireproxyfactory factory = new Xfireproxyfactory (); try {///through Proxyfactory, use the service model ServiceModel and the service endpoint URL (to get the WSDL)//Get a local proxy for the service, this proxy is the actual client Kingteamservic E client = (kingteamservice) factory.create (ServiceModel, serviceurl);/** * Invoke the service * @see Call service       Webservcie/int serviceresponse = Client.jian (12,3) in the local agent (that is, the actual client) that we need. System.out.println (serviceresponse);} Catch (Malformedurlexception e)                 {E.printstacktrace ();} }}


Ii. creating a dynamic client with a WSDL address

public class Kingteamclient {public static void main (String args []) {try {client client = new Client (The new URL ("http://local HOST:80/XFIRED/SERVICES/KINGTEAMSERVICE?WSDL ")); object[] k = Client.invoke (" Jia ", New object[]{12,12}); System.out.println (K[0]);} catch (Malformedurlexception e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ()}}}

Third, Web page javascript

Javascirpt Code:

<script type= "Text/javascript" >function invokeserverfunction () {var xmlHttp;          try{xmlHttp = new XMLHttpRequest ();              }catch (e) {try{xmlHttp = new ActiveXObject ("Msxml2.xmlhttp");                }catch (e) {try{xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");                }catch (e) {alert ("Unfortunately, your browser does not support Ajax, please use a different browser, such as Firefox, Safari, opera8.0+");                  return false;              }}} Xmlhttp.onreadystatechange = function () {if (4 = = xmlhttp.readystate) {              alert (xmlhttp.status);                  if (xmlhttp.status) {//document.writeln ("The Web service Returns the result:" + xmlhttp.responsetext);              document.getElementById ("Result"). InnerHTML = Xmlhttp.responsetext;      }          }      };      var data;            data = ' <?xml version= ' 1.0 "encoding=" UTF-8 "?>"; Over hereNote that it is not recommended to write the following, otherwise it will be error//data = data + ' <soap:envelope xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"//xmlns      : soap= "http://schemas.xmlsoap.org/soap/envelope/" > "; It's best to write these three things as a line of data = Data + ' <soap:envelope xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "htt      P://www.w3.org/2001/xmlschema "xmlns:soap=" http://schemas.xmlsoap.org/soap/envelope/">";          data = data + ' <soap:Body> '; Here the <sayHello> tag, corresponding to the client to invoke the service side of the method data = Data + ' 

Page HTML code:

<body>  <input type= "button" value= "ViewResult" onclick= "invokeserverfunction ()"  /><br/> The result returned by the <br/>web service is: <span id= "Result" ></span>    </body>


Client calls Xfire WebService (with JavaScript)

Related Article

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.