axis2调用天气的webservice

package com.webservice.test;
	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 Test {
	    private static String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";
	    //端点引用 指接口位置
	    private static EndpointReference targetEpr = new EndpointReference(url);
	    //有抽象OM工厂获取OM工厂,创建request SOAP包
	    private static OMFactory fac = OMAbstractFactory.getOMFactory();
	   
	    public static OMElement getOMMethod(String methodStr,String namespace,String tns,String[] pars,String[] vals){
	        //创建命名空间
	        OMNamespace nms = fac.createOMNamespace(namespace, tns);
	        //创建OMElement方法 元素,并指定其在nms指代的名称空间中
	        OMElement method = fac.createOMElement(methodStr, nms);
	        //添加方法参数名和参数值
	        for(int i=0;i<pars.length;i++){
	            //创建方法参数OMElement元素
	            OMElement param = fac.createOMElement(pars[i],nms);
	            //设置键值对 参数值
	            param.setText(vals[i]);
	            //讲方法元素 添加到method方法元素中
	            method.addChild(param);
	        }
	        return method;
	    }
	   
	    public static Options getClientOptions(String action){
	        //创建request soap包 请求选项
	        Options options = new Options();
	        //设置options的soapAction
	        options.setAction(action);
	        //设置request soap包的端点引用(接口地址)
	        options.setTo(targetEpr);
	        //如果报错提示Content-Length,请求内容长度
	        options.setProperty(HTTPConstants.CHUNKED,"false");//把chunk关掉后,会自动加上Content-Length。
	        return options;
	    }




	    public static OMElement getWeather(String action,String methodStr,String namespace,String tns,String[] pars,String[] vals){
	        OMElement result = null;
	        try {
	            ServiceClient client = new ServiceClient();
	            client.setOptions(getClientOptions(action));
	            result =  client.sendReceive(getOMMethod(methodStr,namespace,tns,pars,vals));
	        } catch (AxisFault e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        return result;
	    }




	  public static void main(String[] args) {
	        String action  = "http://WebXml.com.cn/getWeatherbyCityName";
	        String methodStr = "getWeatherbyCityName";
	        String namespace = "http://WebXml.com.cn/";
	        String tns = "xsd";
	        String[] pars = {"theCityName"};
	        String[] vals = {"太原"};
	        OMElement result = null;
	        result = getWeather(action, methodStr, namespace, tns, pars, vals);
	        System.out.println(result);
	    }
	   
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值