WebService - Client调用(Axis2-Document)

简介: WebService - Client调用(Axis2-Document)

采用的免费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());  
            }  
        }  
    }  
}

输出结果如下:


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

 /** 
  * 为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 依赖:

//版本号
<axis2.version>1.6.2</axis2.version>
<jaxws.version>2.2.10</jaxws.version>
<policy.version>2.3.1</policy.version>
<gmbal.version>3.0.0-b023</gmbal.version>
<!-- axis2 - webservice -->
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-aar-maven-plugin</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-adb</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-adb-codegen</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-ant-plugin</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-clustering</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-codegen</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-integration</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-java2wsdl</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-java2wsdl-maven-plugin</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-jaxbri</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-jaxws</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-jaxws-integration</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-jibx</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-json</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-kernel</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-mar-maven-plugin</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-metadata</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-mtompolicy</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-repo-maven-plugin</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-resource-bundle</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-saaj</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-soapmonitor-servlet</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-spring</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-transport-http</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-transport-local</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-wsdl2code-maven-plugin</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-xmlbeans</artifactId>
   <version>${axis2.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>org.apache.axis2.osgi</artifactId>
   <version>${axis2.version}</version>
</dependency>
<!-- jax-ws -->
<dependency>  
    <groupId>com.sun.xml.ws</groupId>  
    <artifactId>jaxws-rt</artifactId>  
    <version>${jaxws.version}</version>  
</dependency>  
<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-tools</artifactId>
    <version>${jaxws.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>rt</artifactId>
    <version>${jaxws.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>policy</artifactId>
    <version>${policy.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.gmbal</groupId>
    <artifactId>gmbal-api-only</artifactId>
    <version>${gmbal.version}</version>
</dependency>


目录
相关文章
|
4月前
WebService - Client调用(Axis2-RPC)
WebService - Client调用(Axis2-RPC)
67 0
|
4月前
|
XML Java 应用服务中间件
WebService - Axis2基于JAX-WS开发WebService并发布多个WebService
WebService - Axis2基于JAX-WS开发WebService并发布多个WebService
70 0
|
XML JavaScript Java
Axis调用Webservice添加SoupHeader
Axis调用Webservice添加SoupHeader
173 0
Axis调用Webservice添加SoupHeader
|
前端开发 JavaScript 编译器
第三十三章 使用 CSP 进行基于标签的开发 - 使用Hyperevents #server和#call调用服务器端方法
第三十三章 使用 CSP 进行基于标签的开发 - 使用Hyperevents #server和#call调用服务器端方法
102 0
|
Java Apache 网络架构
Webservice调用方式:axis,soap详解
转自:[url] http://blog.csdn.net/baiboy4493/archive/2009/03/13/3987526.aspx [/url]  调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis...
1531 0
|
XML Java 数据格式
web service axis2 参数为对象传递
web service axis2 参数为对象传递
109 0