WebService之客户端访问服务端样例
2018-02-10 14:09
417 查看
public class SynOrgUserClient {
//以下是写了一个测试方法,对其进行测试;
public static void main(String[] args) {
//所需参数:webserviceUrl地址;命名空间;访问方法名;参数名;参数值;
String wsdlUrl = "http://localhost:8080/xxxx/services/SynOrgUserService?wsdl";
String targetNamespace = "http://amsuri.org/";
String methodName = "trans_InstitutionalUsersInfo";
String parmName = "requestXml";//requestXml均可,此为形参;
String parmValue = "";
//---------------以下是xml测试案例,为拼接xml字符串而写,适度参考-----------------------------------
SAXReader sax = new SAXReader();// 创建一个SAXReader对象
File xmlFile = new File("d:\\user.xml");// 根据指定的路径创建file对象
Document document = null;
try {
document = sax.read(xmlFile);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}// 获取document对象,如果文档无节点,则会抛出Exception提前结束
Element root = document.getRootElement();// 获取根节点
String rootStr = root.asXML();
//------------------------------------------------------------------------------------------------------
try {
Object obj = callWs(wsdlUrl,targetNamespace,methodName,parmName,rootStr);
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object callWs(String wsdlUrl,String targetNamespace,String methodName,String parmName,String parmValue) throws Exception{
Object res = "";
try{
QName qName = org.apache.axis.Constants.XSD_STRING;
ParameterDesc param = new ParameterDesc(new QName(targetNamespace, parmName), ParameterDesc.IN, qName, java.lang.String.class, false, false);
param.setNillable(true);
OperationDesc oper = new OperationDesc();
oper.setName(methodName);
oper.addParameter(param);
oper.setReturnType(qName);
oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
oper.setUse(org.apache.axis.constants.Use.LITERAL);
Service service = new Service();
Call _call = (Call) service.createCall();
_call.setTargetEndpointAddress(wsdlUrl);
_call.setOperation(oper);
_call.setUseSOAPAction(true);
String actionURI = targetNamespace.endsWith("/")?targetNamespace:targetNamespace+"/";
_call.setSOAPActionURI(actionURI+methodName);
_call.setEncodingStyle(null);
_call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
_call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new QName(targetNamespace, methodName));
_call.setTimeout(60000);
res = _call.invoke(new Object[]{parmValue});
}catch(Exception e){
throw new Exception("调用WebService失败,错误信息:"+e.getMessage());
}
return res;
}
}
//以下是写了一个测试方法,对其进行测试;
public static void main(String[] args) {
//所需参数:webserviceUrl地址;命名空间;访问方法名;参数名;参数值;
String wsdlUrl = "http://localhost:8080/xxxx/services/SynOrgUserService?wsdl";
String targetNamespace = "http://amsuri.org/";
String methodName = "trans_InstitutionalUsersInfo";
String parmName = "requestXml";//requestXml均可,此为形参;
String parmValue = "";
//---------------以下是xml测试案例,为拼接xml字符串而写,适度参考-----------------------------------
SAXReader sax = new SAXReader();// 创建一个SAXReader对象
File xmlFile = new File("d:\\user.xml");// 根据指定的路径创建file对象
Document document = null;
try {
document = sax.read(xmlFile);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}// 获取document对象,如果文档无节点,则会抛出Exception提前结束
Element root = document.getRootElement();// 获取根节点
String rootStr = root.asXML();
//------------------------------------------------------------------------------------------------------
try {
Object obj = callWs(wsdlUrl,targetNamespace,methodName,parmName,rootStr);
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object callWs(String wsdlUrl,String targetNamespace,String methodName,String parmName,String parmValue) throws Exception{
Object res = "";
try{
QName qName = org.apache.axis.Constants.XSD_STRING;
ParameterDesc param = new ParameterDesc(new QName(targetNamespace, parmName), ParameterDesc.IN, qName, java.lang.String.class, false, false);
param.setNillable(true);
OperationDesc oper = new OperationDesc();
oper.setName(methodName);
oper.addParameter(param);
oper.setReturnType(qName);
oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
oper.setUse(org.apache.axis.constants.Use.LITERAL);
Service service = new Service();
Call _call = (Call) service.createCall();
_call.setTargetEndpointAddress(wsdlUrl);
_call.setOperation(oper);
_call.setUseSOAPAction(true);
String actionURI = targetNamespace.endsWith("/")?targetNamespace:targetNamespace+"/";
_call.setSOAPActionURI(actionURI+methodName);
_call.setEncodingStyle(null);
_call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
_call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new QName(targetNamespace, methodName));
_call.setTimeout(60000);
res = _call.invoke(new Object[]{parmValue});
}catch(Exception e){
throw new Exception("调用WebService失败,错误信息:"+e.getMessage());
}
return res;
}
}
相关文章推荐
- WebService XFire获得访问服务端的客户端IP
- Android客户端WebService访问接口(Ksoap2Android调用.net服务端)
- WebService:ws客户端(JDK1.4)访问ws服务端(JDK1.6)
- WebService服务端、客户端编码,以及发布、访问
- TCP/IP?UDP编程之客户端访问服务端的数据库(数据库配置)
- webservice客户端引用服务端程序详解
- axis1.4发布返回复杂类型的webservice服务+代码访问+客户端访问
- 分享一个PHP写的简单webservice服务端+客户端
- webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客户端(一)
- webservice的服务端和客户端的创建
- webService服务端和客户端的创建
- 基于Xfire SOAP Header的WebService安全验证(xfire服务端,jax-ws和xfire两种客户端)
- Axis创建webservice客户端和服务端
- ASP.NET AJAX初学体验之客户端访问WebService(2)
- WebService 服务端客户端 实例 HTTPRIO (一) SOAP WSDL
- ofbiz的webservice接口提供(3)-不规范的wsdl的客户端访问代码
- 开发webservice服务端和客户端(cxf,xfire)
- C++客户端访问WebService
- Axis1.4 快速入门 发布WebService服务端、生成WebService客户端
- DCOM服务端及客户端指定用户访问的安全设置