Webservice客户端调用--java+axis2-document

webservice有多种客户端调用方式,我们这里说的是通过axis2框架的document方式。其他方式后续补充。

我们使用免费的webservice地址

相关依赖,其中版本号可以改变。

        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>1.7.8</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-transport-local -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>1.7.8</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-transport-http -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.7.8</version>
        </dependency>

代码如下

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 {
	/*
	 * Axis2-Document,手动调用
	 */
	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);  
  }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值