1、编写代码方式:
packagecom.yudun.test;
importjava.rmi.RemoteException;
importorg.apache.axis.client.Call;
importorg.apache.axis.client.Service;
importorg.apache.axis.message.PrefixedQName;
importorg.apache.axis.message.SOAPHeaderElement;
importcom.cezanne.golden.user.Exception;
importcom.cezanne.golden.user.UserManagerServiceProxy;
importjavax.xml.namespace.QName;
importjava.net.MalformedURLException;
importjavax.xml.rpc.ServiceException;
importjavax.xml.soap.Name;
importjavax.xml.soap.SOAPException;
publicclasstestWebService{
publicstaticStringgetResult()throwsServiceException,MalformedURLException,RemoteException,SOAPException
{
//标识WebService的具体路径
Stringendpoint="WebService服务地址";
//创建Service实例
Serviceservice=newService();
//通过Service实例创建Call的实例
Callcall=(Call)service.createCall();
//将WebService的服务路径加入到call实例之中.
call.setTargetEndpointAddress(newjava.net.URL(endpoint));//为Call设置服务的位置
//由于需要认证,故需要设置调用的SOAP头信息。
NameheaderName=newPrefixedQName(newQName("发布的wsdl里的targetNamespace里的url","string_itemName"));
org.apache.axis.message.SOAPHeaderElementheader=newSOAPHeaderElement(headerName);
header.addTextNode("blablabla");
call.addHeader(header);
//SOAPHeaderElementsoapHeaderElement=newSOAPHeaderElement("发布的wsdl里的targetNamespace里的url","SoapHeader");
//soapHeaderElement.setNamespaceURI("发布的wsdl里的targetNamespace里的url");
//try
//{
//soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");
//}
//catch(SOAPExceptione)
//{
//e.printStackTrace();
//}
//call.addHeader(soapHeaderElement);
//调用WebService的方法
org.apache.axis.description.OperationDescoper;
org.apache.axis.description.ParameterDescparam;
oper=neworg.apache.axis.description.OperationDesc();
oper.setName("opName");
param=neworg.apache.axis.description.ParameterDesc(newjavax.xml.namespace.QName("","arg0"),org.apache.axis.description.ParameterDesc.IN,newjavax.xml.namespace.QName("
param.setOmittable(true);
oper.addParameter(param);
param=neworg.apache.axis.description.ParameterDesc(newjavax.xml.namespace.QName("","arg1"),org.apache.axis.description.ParameterDesc.IN,newjavax.xml.namespace.QName("
param.setOmittable(true);
oper.addParameter(param);
param=neworg.apache.axis.description.ParameterDesc(newjavax.xml.namespace.QName("","arg2"),org.apache.axis.description.ParameterDesc.IN,newjavax.xml.namespace.QName("
param.setOmittable(true);
oper.addParameter(param);
oper.setReturnType(newjavax.xml.namespace.QName("
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(newjavax.xml.namespace.QName("","return"));
oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
oper.setUse(org.apache.axis.constants.Use.LITERAL);
oper.addFault(neworg.apache.axis.description.FaultDesc(
newjavax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url","Exception"),
"Exception",
newjavax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url","Exception"),
true
));
call.setOperation(oper);
call.setOperationName(newjavax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url","opName"));
//调用WebService,传入参数
Stringres=(String)call.invoke(newObject[]("arg0","arg1"));
System.out.println("===============");
returnres;
}
publicstaticvoidmain(String[]args){
try{
System.out.println(getResult());
}catch(MalformedURLExceptione){
e.printStackTrace();
}catch(RemoteExceptione){
e.printStackTrace();
}catch(ServiceExceptione){
e.printStackTrace();
}catch(SOAPExceptione){
e.printStackTrace();
}
}
}
2、利用Eclipse自动生成WebServiceclient代码就容易多了:(由于还不会发图片,就用语言描述了,大家酬和看吧。。。)
首先,newproject,选择other,在输入框中输入WebServiceClient,选中搜索后的结果,点击Next,在Servicedefinition中输入WebService的发布地址,点击Finish
这样,WebServiceClient代码已经生成好了。
接下来写一个Test类,在main函数中输入如下代码:
Stringendpoint="服务器的WebService的地址";
YourWebServiceNameProxyumsp=newYourWebServiceNameProxy(endpoint);
try{
StringresultStr=umsp.opMethod("arg0","arg1");
System.out.println(resultStr);
}catch(Exceptione){
System.out.println("异常");
e.printStackTrace();
}catch(RemoteExceptione){
System.out.println("RemoteException异常");
e.printStackTrace();
}