调用webservice例子代码

时间:2021-12-23 18:54:01

                                        调用webservice例子代码

 

 

 

平台:Nutz

 

 

后台调用:

 

    @At
@Ok("json")
public Object getPersonInfo(String idCard){

String result = "";
try {
result = getWebServiceResult("http://localhost:8080/sjsjzx/services/SjrkkService?wsdl", "getPersonInfo", idCard);
} catch (Exception e) {
e.printStackTrace();
}

return result;
}

/**
* 调用webservice的方法.
* @param url wsdl链接
* @param method 调用的接口
* @param idCard 传入的参数
* @return
* @throws Exception
*/
private String getWebServiceResult( String url, String method, String idCard) throws Exception{
String rtnXml = null;
try {
String endpoint = url;
Service service = new Service();
Call call = (Call) service.createCall();

call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(method);

rtnXml = (String) call.invoke(new Object[]{idCard});


} catch (Exception e) {
e.printStackTrace();
}
return rtnXml;
}


jsp测试:

 

//测试调用webservice
function getPersonInfo(idCard){

$.post(
"${ctx}/person/getPersonInfo?idCard="+idCard,
{Action:"post"},
function(data,textStatus){
alert(data);
},
"json"
);
}