WebService - Client调用(Axis2-Document)

采用RPC的方式参考博文:Client调用(Axis2-RPC)。

采用的免费webservice接口:

http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

客户端调用代码如下:

package com.web.hh.constroller;

import java.util.Iterator;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class ClientWeather {
	/*
	 * 第二种方式,手动调用
	 */
	public static void main(String[] args) throws AxisFault {
		ServiceClient serviceClient = new ServiceClient();  
        Options option = new Options();
        option.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);  
        option.setTransportInProtocol(Constants.TRANSPORT_HTTP);  
        option.setAction("http://WebXml.com.cn/getWeather");  
        // 值为targetNamespace+methodName
        EndpointReference epfs = new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");  
        option.setTo(epfs);  
        serviceClient.setOptions(option);  
        
        OMFactory fac = OMAbstractFactory.getOMFactory();  
        OMNamespace namespace = fac.createOMNamespace("http://WebXml.com.cn/", "");  
        OMElement element = fac.createOMElement("getWeather", namespace);  
        OMElement theCityCode = fac.createOMElement("theCityCode ", namespace);  
        theCityCode.setText("北京");  
        element.addChild(theCityCode);  
        OMElement theUserID = fac.createOMElement("theUserID ", namespace);  
        theUserID.setText("");  
        element.addChild(theUserID);  
  
        OMElement result = serviceClient.sendReceive(element);  
        System.out.println(result);   
        System.out.println("****************************************************************************************************************");
        Iterator in = result.getChildrenWithLocalName("getWeatherResult");  
        while(in.hasNext()){  
            OMElement om = (OMElement)in.next();  
            Iterator in2 = om.getChildElements();  
            while(in2.hasNext()){  
//                System.out.println(in2.next().toString());  
                System.out.println(((OMElement)in2.next()).getText());  
            }  
        }  
    }  
}


输出结果如下:

WebService - Client调用(Axis2-Document)_第1张图片


上面服务是不需要验证的,如果需要验证,参考代码如下:

 /** 
  * 为SOAP Header构造验证信息, 
  * 如果你的服务端是没有验证的,那么你不用在Header中增加验证信息 
  * 
  * @param serviceClient 
  * @param tns 命名空间 
  * @param user 
  * @param passwrod 
  */  
  public void addValidation(ServiceClient serviceClient, String tns , String user, String passwrod) {  
    OMFactory fac = OMAbstractFactory.getOMFactory();  
    OMNamespace omNs = fac.createOMNamespace(tns, "nsl");  
    OMElement header = fac.createOMElement("AuthenticationToken", omNs);  
    OMElement ome_user = fac.createOMElement("Username", omNs);  
    OMElement ome_pass = fac.createOMElement("Password", omNs);  
      
    ome_user.setText(user);  
    ome_pass.setText(passwrod);  
      
    header.addChild(ome_user);  
    header.addChild(ome_pass);  
  
    serviceClient.addHeader(header);  
  }  

网友建议贴出相关jar的pom文件,因为时间比较久,pom文件中依赖不太确定,遇到具体问题望再具体解决,也可留言。

下面是相关的pom 依赖:

//版本号
1.6.2
2.2.10
2.3.1
3.0.0-b023


	 org.apache.axis2
	 axis2
	 ${axis2.version}


	 org.apache.axis2
	 axis2-aar-maven-plugin
	 ${axis2.version}


	 org.apache.axis2
	 axis2-adb
	 ${axis2.version}


	 org.apache.axis2
	 axis2-adb-codegen
	 ${axis2.version}


	 org.apache.axis2
	 axis2-ant-plugin
	 ${axis2.version}


	 org.apache.axis2
	 axis2-clustering
	 ${axis2.version}


	 org.apache.axis2
	 axis2-codegen
	 ${axis2.version}


	 org.apache.axis2
	 axis2-integration
	 ${axis2.version}


	 org.apache.axis2
	 axis2-java2wsdl
	 ${axis2.version}


	 org.apache.axis2
	 axis2-java2wsdl-maven-plugin
	 ${axis2.version}


	 org.apache.axis2
	 axis2-jaxbri
	 ${axis2.version}


	 org.apache.axis2
	 axis2-jaxws
	 ${axis2.version}


	 org.apache.axis2
	 axis2-jaxws-integration
	 ${axis2.version}


	 org.apache.axis2
	 axis2-jibx
	 ${axis2.version}


	 org.apache.axis2
	 axis2-json
	 ${axis2.version}


	 org.apache.axis2
	 axis2-kernel
	 ${axis2.version}


	 org.apache.axis2
	 axis2-mar-maven-plugin
	 ${axis2.version}


	 org.apache.axis2
	 axis2-metadata
	 ${axis2.version}


	 org.apache.axis2
	 axis2-mtompolicy
	 ${axis2.version}


	 org.apache.axis2
	 axis2-repo-maven-plugin
	 ${axis2.version}


	 org.apache.axis2
	 axis2-resource-bundle
	 ${axis2.version}


	 org.apache.axis2
	 axis2-saaj
	 ${axis2.version}


	 org.apache.axis2
	 axis2-soapmonitor-servlet
	 ${axis2.version}


	 org.apache.axis2
	 axis2-spring
	 ${axis2.version}


	 org.apache.axis2
	 axis2-transport-http
	 ${axis2.version}


	 org.apache.axis2
	 axis2-transport-local
	 ${axis2.version}


	 org.apache.axis2
	 axis2-wsdl2code-maven-plugin
	 ${axis2.version}


	 org.apache.axis2
	 axis2-xmlbeans
	 ${axis2.version}


	 org.apache.axis2
	 org.apache.axis2.osgi
	 ${axis2.version}



  
    com.sun.xml.ws  
    jaxws-rt  
    ${jaxws.version}  
  

    com.sun.xml.ws
    jaxws-tools
    ${jaxws.version}


    com.sun.xml.ws
    rt
    ${jaxws.version}


    com.sun.xml.ws
    policy
    ${policy.version}


    org.glassfish.gmbal
    gmbal-api-only
    ${gmbal.version}

你可能感兴趣的:(WebService)