【Java】webservice调用

最近一个项目要调用.net开发的webservice服务,研究好一阵才调试通过,用了两种调用方式,记录一下。

EndpointReference targetEPR = new EndpointReference(getUrl(referToOrgCode));// 指定调用WebService的URL
Map<String, Object> objMap = new HashMap<String, Object>(); if(referToOrgCode.equals("12152501460785224W")){ //调用.net发布的webservice Options options = new Options(); options.setTimeOutInMilliSeconds(3*60*1000); //设置超时时间 options.setAction(getNs(referToOrgCode) + getMethodName(referToOrgCode));//设置action options.setTo(targetEPR); options.setProperty(HTTPConstants.CHUNKED, "false");// 设置不受限制 ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace(getNs(referToOrgCode), ""); OMElement method = fac.createOMElement(getMethodName(referToOrgCode), omNs); OMElement name = fac.createOMElement("reqParam", omNs);// 设置入参名称 name.setText(methodParam);// 设置入参值 method.addChild(name); method.build(); OMElement response = sender.sendReceive(method); OMElement elementReturn = response.getFirstElement(); objMap.put("resultContent", elementReturn.getText()); }else{ RPCServiceClient client = new RPCServiceClient(); Options options = client.getOptions(); options.setTimeOutInMilliSeconds(3*60*1000); options.setTo(targetEPR); options.setAction(getNs(referToOrgCode) + getMethodName(referToOrgCode)); // 指定service方法的参数值 Object[] parmas = new Object[] { methodParam.toString() }; // 指定service方法返回值的数据类型的Class对象 Class<?>[] classes = new Class[] { String.class }; // 指定要调用的service方法及WSDL文件的命名空间 // QName opAddEntry = new QName(getNs(referToOrgCode), // getMethodName(referToOrgCode)); QName opAddEntry = new QName(getNs(referToOrgCode), getMethodName(referToOrgCode), getPrefix(referToOrgCode)); // 调用service方法并输出该方法的返回值 Object[] str = client.invokeBlocking(opAddEntry, parmas, classes); objMap.put("resultContent", str[0].toString()); }

  依赖:

<version.axiom>1.2.13</version.axiom>
<version.axis2>1.6.2</version.axis2>
<dependency>
    <groupId>org.apache.ws.commons.axiom</groupId>
    <artifactId>axiom-api</artifactId>
    <version>${version.axiom}</version>
</dependency>
<dependency>
    <groupId>org.apache.ws.commons.axiom</groupId>
    <artifactId>axiom-impl</artifactId>
    <version>${version.axiom}</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-adb</artifactId>
    <version>${version.axis2}</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-kernel</artifactId>
    <version>${version.axis2}</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-http</artifactId>
    <version>${version.axis2}</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-local</artifactId>
    <version>${version.axis2}</version>
</dependency>

 

posted @ 2018-11-22 14:24  韩小韩Sunny  阅读(407)  评论(0编辑  收藏  举报