webService 客户端调用 axis2


今天找出来给同事用,重新改了下 给同事.  

package com.jielan.axis2;

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.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;

public class Client {
    //private static String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
    // targetEPR指定打包的Service(.aar文件)在容器中的物理位置。
    //EndpointReference端点引用
    private static EndpointReference targetEpr;
    //创建request SOAP包工厂 fac。
    private static OMFactory fac = OMAbstractFactory.getOMFactory();
    
    public  static OMElement getMethodOMElement(String nameSpace,String tns,String methodName,String[] args,String[] vals){
        // OMNamespace指定此fac SOAP文档名称空间。
        //有一个tns的namespace,这个namespace和目标的接口有关,一定要和目标 wsdl的message传输接口一致 是tns
        OMNamespace omNs = fac.createOMNamespace(nameSpace,tns);
        //创建元素getWeatherbyCityName oap,并指定其在omNs指代的名称空间中。  方法函数
        OMElement method = fac.createOMElement(methodName,omNs);
        //给method添加 这个方法的参数名,和参数值
        for(int i =0;i<args.length;i++){
            //新建参数OMElement对象实例,设置参数名
            OMElement params = fac.createOMElement(args[i],omNs);
            //给参数名对象设置参数值
            params.setText(vals[i]);
            //将参数OMElement对象添加到Method对象中
            method.addChild(params);
        }
        //返回request的方法 SOAP包
        return method;
    }
    
    public static void main(String[] args) throws AxisFault {
        OMElement result = new Client().getResult("http://**********/EnterpriseService.asmx?wsdl",
                "http://tempuri.org/method", "http://tempuri.org/", "tns", "method",
                new String[]{"paramName"}, new String[]{"value"},60000);
        System.out.println(result.toString());
    }
    
    /****
     *
     * @param url webService wsdl地址
     * @param SOAPAction SOAPAction的地址在方法中 请求头里面有SOAPAction: "http://tempuri.org/*******"
     * @param nameSpace  命名空间  xmlns="http://tempuri.org/"
     * @param tns  为空好像也可以 我是用 "tns"
     * @param method 方法名
     * @param params 方法传入参数名称
     * @param values 方法传入参数值
     * @return 返回结果 OMElement 类型
     * @throws AxisFault
     */
    public OMElement getResult(String url,String SOAPAction,String nameSpace,String tns,String method,String[] params,String[] values,int waitTime) throws AxisFault{
       targetEpr = new EndpointReference(url);
        ServiceClient client = new ServiceClient();
        Options options = new Options();
        options.setTimeOutInMilliSeconds(waitTime);
        options.setAction(SOAPAction);
        options.setTo(targetEpr);
        options.setTransportInProtocol(Constants.TRANSPORT_HTTP);//设定传输协议
        options.setProperty(HTTPConstants.CHUNKED, "false");
        client.setOptions(options);
        OMElement method1= getMethodOMElement(nameSpace,tns,method,params,values);
        OMElement result = client.sendReceive(method1);
        return result;
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值