下面的代码描述了一个简单的客户端调用webservice的测试代码
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import com.demo.webservice.WebServiceParam;
/**
* <p> Title: webservice测试公共类</p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2012 2012-2-13</p>
* <p> Company: pccw</p>
* @author xuwh
* @version 1.0
*/
public class WebServiceClientTest {
public static void main(String args[])
{
// 服务端webservice地址
String endpoint = "http://localhost:8080/sshdemo/services/UserWebService";
// 定义服务端webservice调用的方法名称opName
String opName = "getUserNameById";
WebServiceParam[] params = new WebServiceParam[1];
// 可以定义多个WebServiceParam对象表示多个参数
WebServiceParam p1 = new WebServiceParam();
// 定义服务端webservice调用方法的参数名称以及值
p1.setParamName("userId");
p1.setParamValue("402881cc3564decb01356510df090004");
QName qName = org.apache.axis.encoding.XMLType.XSD_STRING;
p1.setqName(qName);
params[0] = p1;
String str="";
try {
str = getStringByWebService(endpoint, opName, params);
System.out.println("==webService Result==" + str);
} catch (Exception e) {
str = "异常";
System.out.println("==webService error=str=" + str);
}
}
/**
* <p>
* Description:调用返回值是string类型的webservice
* <p>
* @param endpoint
* @param opName
* @param params
*/
public static String getStringByWebService(String endpoint, String opName,
WebServiceParam[] params) throws Exception {
try {
String res = null;
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
for (int i = 0; i < params.length; i++) {
call.addParameter(params[i].getParamName(), params[i].getqName(),
javax.xml.rpc.ParameterMode.IN);
}
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setOperationName(new QName("http://localhost:8080/sshdemo/services/" + opName,
opName));
call.setUseSOAPAction(true);
//call.setSOAPActionURI("http://localhost:8080/sshdemo/services/" + opName + "/Rpc");
call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
Object o[] = new Object[params.length];
for (int i = 0; i < params.length; i++) {
o[i] = params[i].getParamValue();
}
res = (String) call.invoke(o);
return res;
} catch (Exception e) {
throw e;
}
}
}
![](/qrcode.jpg)
/**
* <p> Title: 调用webservice的参数配置对象</p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2012 2012-2-13</p>
* @version 1.0
*/
public class WebServiceParam {
private String paramName;
private Object paramValue;
private QName qName;
public WebServiceParam(){}
public WebServiceParam(String paramName,String paramValue,QName qName){
this.paramName = paramName;
this.paramValue = paramValue;
this.qName = qName;
}
public QName getqName() {
return qName;
}
public void setqName(QName qName) {
this.qName = qName;
}
public String getParamName() {
return paramName;
}
public void setParamName(String paramName) {
this.paramName = paramName;
}
public Object getParamValue() {
return paramValue;
}
public void setParamValue(Object paramValue) {
this.paramValue = paramValue;
}
}