Java call WebService can use the Axis.jar provided by Apache to write its own code, or use Eclipse to automatically generate WebService client code, using the proxy class to invoke. The theory is the same, but it's easier to automatically generate code with eclipse. 1, write code way: Package Com.yudun.test;import java.rmi.RemoteException;
Import Org.apache.axis.client.Call;
Import Org.apache.axis.client.Service;
Import Org.apache.axis.message.PrefixedQName;
Import Org.apache.axis.message.soapheaderelement;import com.cezanne.golden.user.Exception;
Import Com.cezanne.golden.user.usermanagerserviceproxy;import javax.xml.namespace.QName;
Import Java.net.malformedurlexception;import javax.xml.rpc.ServiceException;
Import Javax.xml.soap.Name;
Import javax.xml.soap.SOAPException;
public class TestWebService {public static String GetResult () throws Serviceexception, Malformedurlexception, Remoteexce Ption, SoapException
{
Identify the specific path to the Web service
String endpoint = "WebService service address";
Create a service instance
Service service = new service ();
To create an instance of call from a service instance
Call Call Service.createcall ();
Add the service path of the Web service to the call instance.
Call.settargetendpointaddress (new Java.net.URL (endpoint));//Set the location of the service for call//Because authentication is required, you need to set the SOAP header information to be called.
Name headername = new Prefixedqname ("url in targetnamespace in the published WSDL", "String_itemname"));
Org.apache.axis.message.SOAPHeaderElement Header = new Soapheaderelement (headername);
Header.addtextnode ("Blablabla");
Call.addheader (header);
Soapheaderelement soapheaderelement = new soapheaderelement ("url in targetnamespace in the published WSDL", "SoapHeader");
Soapheaderelement.setnamespaceuri ("url in the targetnamespace in the published WSDL");
Try
// {
Soapheaderelement.addchildelement ("String_itemname"). SetValue ("Blablabla");
// }
catch (SoapException E)
// {
E.printstacktrace ();
// }
Call.addheader (soapheaderelement); Methods for invoking Web service
Org.apache.axis.description.OperationDesc Oper;
Org.apache.axis.description.ParameterDesc param;
Oper = new Org.apache.axis.description.OperationDesc ();
Oper.setname ("Opname");
param = new Org.apache.axis.description.ParameterDesc (New Javax.xml.namespace.QName ("", "arg0"), Org.apache.axis.description.ParameterDesc.IN, New Javax.xml.namespace.QName ("Http://www.w3.org/2001/XMLSchema", " String "), Java.lang.String.class, False, false);
Param.setomittable (TRUE);
Oper.addparameter (param);
param = new Org.apache.axis.description.ParameterDesc (New Javax.xml.namespace.QName ("", "arg1"), Org.apache.axis.description.ParameterDesc.IN, New Javax.xml.namespace.QName ("Http://www.w3.org/2001/XMLSchema", " String "), Java.lang.String.class, False, false);
Param.setomittable (TRUE);
Oper.addparameter (param);
param = new Org.apache.axis.description.ParameterDesc (New Javax.xml.namespace.QName ("", "arg2"), Org.apache.axis.description.ParameterDesc.IN, New Javax.xml.namespace.QName ("Http://www.w3.org/2001/XMLSchema", " String "), Java.lang.String.class, False, false);
Param.setomittable (TRUE);
Oper.addparameter (param);
Oper.setreturntype (New Javax.xml.namespace.QName ("Http://www.w3.org/2001/XMLSchema", "string"));
Oper.setreturnclass (Java.lang.String.class);
Oper.setreturnqname (New Javax.xml.namespace.QName ("", "return"));
Oper.setstyle (Org.apache.axis.constants.Style.WRAPPED);
Oper.setuse (Org.apache.axis.constants.Use.LITERAL);
Oper.addfault (New Org.apache.axis.description.FaultDesc (
New Javax.xml.namespace.QName ("url in targetnamespace in the published WSDL", "Exception"),
"Exception",
New Javax.xml.namespace.QName ("url in targetnamespace in the published WSDL", "Exception"),
True
));
Call.setoperation (oper);
Call.setoperationname (New Javax.xml.namespace.QName ("url in targetnamespace in the published WSDL", "Opname")); Invoking Web Service, passing in Parameters
string res = (string) call.invoke (new object[] ("arg0", "arg1"));
System.out.println ("===============");
return res;
} /**
* @param args
*/
public static void Main (string[] args) {
try {
System.out.println (GetResult ());
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (RemoteException e) {
E.printstacktrace ();
} catch (Serviceexception e) {
E.printstacktrace ();
} catch (SoapException e) {
E.printstacktrace ();
}
}} 2, it is much easier to automatically generate WebService Client code with Eclipse: First, new project, select Other, enter the Web Service Client in the input box, select the search results, click Next, Enter the WebService address in the service definition and click Finish
In this way, the WebService client code has been generated well. Next write a test class, enter the following code in the main function: String endpoint = "WebService address of the server";
Yourwebservicenameproxy Umsp = new Yourwebservicenameproxy (endpoint);
try {
String resultstr = Umsp.opmethod ("arg0", "arg1");
System.out.println (RESULTSTR);
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("exception");
E.printstacktrace (); } catch (RemoteException e) {
System.out.println ("RemoteException anomaly");
E.printstacktrace ();
}
Java Call WebService