Now there are several ways to call WebService and summarize:
1, stub mode. In this way, the Wsdl2java tool class of axis is used. This is based on the WSDL generated by the WebService client class. This makes it easy to invoke webservice as if it were a local class. A batch process is required, as follows:
Set Axis_lib=e:\jar\axis-bin-1_4\axis-1_4\lib
Set java_cmd=java-djava.ext.dirs=%axis_lib%
Set OUTPUT_PATH=D:\SRC
Set PACKAGE=COM.HSIPCC.WSDL
%java_cmd%org.apache.axis.wsdl.wsdl2java-o%output_path%-p%package% http://localhost:8000/service/Business?wsdl
This will generate stub source code in the SRC directory of the D drive, which can then be used in the project. However, this method is not flexible, is more stupid, if the service side changes, there is a need to manually change the stub.
2. Dynamic Agent:
2.1, axis1.4.
Package com.aming.client;
import java.net.MalformedURLException;
import Java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import Org.apache.axis.client.Call;
import Org.apache.axis.client.Service;
Public class axis1client {
Public static void main (string[] args) throws Serviceexception,malformedurlexception, remoteexception {
String Path = "Http://localhost:8080/xfire/services/staticService";
Service service = New service ();
Call Call Service.createcall ();
Call.settargetendpointaddress (Thenew URL (path));
Call.setoperationname (new QName (path, "Putinfo"));
Object obj =call.invoke (new object[]{"LadyGaga", "123", "456"});
System.out.println (Obj.tostring ());
}
}
Explain: Write the process:
1. Create an Axis Client Services service
2. Call is created by service (calling is the object to be called)
3. You need to set the properties of the Call object Targetendpointaddress (destination address) and OperationName (the method of action is to invoke the methods, where this method requires QName (for locating the method) object, new QName (Path, "Putinfo") where path is a namespace, Putinfo is a method in this namespace (the method to invoke).
4, the last is called WebService method, invoke, this method needs to pass in the required parameters, and this parameter is passed in the way of an object array. This method returns the return value.
2.2 Axis2
Packagecom.aming.test;
Import Org.apache.axis2.AxisFault;
Importorg.apache.axis2.addressing.EndpointReference;
Import org.apache.axis2.client.Options;
Import Javax.xml.namespace.QName;
Importorg.apache.axis2.rpc.client.RPCServiceClient;
public class Diaoyong {
publicstatic void Main (string[] args) throws Axisfault {
Call WebService using RPC mode
Rpcserviceclient serviceclient = Newrpcserviceclient ();
Options Options =serviceclient.getoptions ();
Specifies the URL to call WebService
EndpointReference Targetepr = newendpointreference (
"Http://localhost:8080/xfire/services/staticService");
Options.setto (TARGETEPR);
Specifying the parameter value of the GetGreeting method
object[] Opaddentryargs = new object[]{"Superman", "123", "456"};
Class object specifying the data type of the GetGreeting method return value
Class[] classes = new Class[]{int.class};
Specify the GetGreeting method to invoke and the namespace of the WSDL file
QName opaddentry = Newqname ("Http://com.hundsun.service/StaticService", "Putinfo");
Call the GetGreeting method and output The return value of the method
System.out.println (Serviceclient.invokeblocking (Opaddentry,opaddentryargs, classes) [0]);
Here's the code that calls the GetPrice method, similar to the code that calls the GetGreeting method
Classes = new class[] {int.class};
QName opaddentry = Newqname ("Http://localhost:8080/axis1/services/MyService", "GetInfo");
System.out.println (Serviceclient.invokeblocking (Opaddentry, newobject[]{}, classes) [0]);
}
}
The comments are not explained in detail.