The client calls webService.

Source: Internet
Author: User

 

1. Write on the client

The client can access the services provided by Web Service in XFire and axis modes.

Prerequisites: the service is started and can run (generate WSDL ).

Note: To facilitate the test, the test class is now put into the same project. Of course, either of the following two methods can be deployed to another new project.

 

1.1 first: Xfire Implementation call

1.1.1 Method 1: Agent Factory

Package com. smt. service. test;

 

Import java.net. MalformedURLException;

 

Import org. codehaus. xfire. XFire;

Import org. codehaus. xfire. XFireFactory;

 

Import org. codehaus. xfire. client. XFireProxyFactory;

Import org. codehaus. xfire. service. Service;

Import org. codehaus. xfire. service. binding. ObjectServiceFactory;

 

Import com. smt. service. IBankingService;

 

/*************************************** *******************

*

* Function Description: </br>

*

* Author: chenab </br>

*

* Creation Time: 05:24:47 </br>

*

* Version: V1.0 </br>

**************************************** ******************/

Public class clientXFire {

Private static final String urls = "http: // 192.168.7.177: 8088/testXFire/services/BankingService ";

Private void testService (){

Try {

Service serviceModel = new ObjectServiceFactory (). create (IBankingService. class );

ServiceModel = new ObjectServiceFactory (). create (IBankingService. class );

XFire xFire = XFireFactory. newInstance (). getXFire ();

XFireProxyFactory factory = new XFireProxyFactory (xFire );

IBankingService service = (IBankingService) factory. create (serviceModel, urls );

Service. transferFunds ("hello ");

} Catch (MalformedURLException e ){

System. out. println ("error! ");

E. printStackTrace ();

}

 

}

 

Public static void main (String [] args) throws MalformedURLException {

ClientXFire c = new clientXFire ();

C. testService ();

}

}

1.1.2 Method 2: Agent Factory

Package com. smt;

 

Import java.net. MalformedURLException;

Import java.net. URL;

 

Import org. codehaus. xfire. client. Client;

Import org. w3c. dom. Document;

 

/*************************************** *******************

*

* Function Description: </br>

*

* Author: chenab </br>

*

* Creation Time: 05:14:25 </br>

*

* Version: V1.0 </br>

**************************************** ******************/

Public class XFireClient {

Private static final String urls = "http: // 192.168.0.3: 3001/smart_webservice/services/SmtDocService? Wsdl ";

 

Public static void main (String [] args) throws MalformedURLException, Exception {

Client client = new Client (new URL (urls ));

Object [] xml = client. invoke ("findSrcDoc", new Object [] {1L });

Document doc = (Document) xml [0];

System. out. println (doc. getDocumentElement (). getTextContent ());

}

}

 

1.2 Second: calling through axis

Since the server is written in Xfire, it is now called using axis. You need to make some modifications on the server.

1.2.1 Step 1: add the package file required by axis

Shows the directory structure after adding the package file required by Axis:

 

 

1.2.2 Step 2: Modify the Web. xml configuration file

Add the AxisServlet of the basic Servlet to web. xml as follows:

 

 

1.2.3 Step 3: Add server-config.wsdd files and modify

 

 

1.2.4 Step 4: Compile the client test code

Package com. smt. service. test;

Import java.net. MalformedURLException;

Import java. rmi. RemoteException;

 

Import javax. xml. rpc. ServiceException;

 

Import org. apache. axis. client. Call;

Import org. apache. axis. client. Service;

 

/*************************************** *******************

*

* Function Description: </br>

*

* Author: chenab </br>

*

* Creation Time: 05:48:31 </br>

*

* Version: V1.0 </br>

**************************************** ******************/

Public class clientAxis {

Private static final String urls = "http: // 192.168.7.177: 8088/testXFire/services/BankingService ";

Public static void main (String [] args ){

Try {

/*

* Banking is the name registered by webservice in services. xml.

*/

Service service = new Service ();

Call call = (Call) service. createCall ();

Call. setTargetEndpointAddress (new java.net. URL (urls ));

 

// Because authentication is required, you need to set the user name and password for the call.

// Call. getMessageContext (). setUsername ("sophia ");

// Call. getMessageContext (). setPassword ("sophia ");

 

/*

* Method name of the service call

*/

Call. setOperationName ("transferFunds ");

/*

* The parameter of the invoke method is an Object array. The number of elements in the array is the same as that of the method parameter.

*/

String translateText = (String) call. invoke (new Object [] {"hello "});

System. out. println (translateText );

} Catch (ServiceException e ){

E. printStackTrace ();

System. out. println ("Service failed to get the Call object! ");

} Catch (MalformedURLException e ){

E. printStackTrace ();

System. out. println ("new java.net. URL (url) error! ");

} Catch (RemoteException e ){

E. printStackTrace ();

System. out. println ("remote error! ");

}

 

}

 

}

 

 

 

 

This article is from the blog "blow cowhide to pull a submarine to push trains and planes ".

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.