Axis2调用C#写的WebService(转自网易http://blog.163.com/cjd_cd/blog/static/46115565200811239239831/)

本文介绍如何使用Axis2在Java中调用C#编写的WebService来获取天气预报信息。示例代码展示了一个Java类ServiceGetWeather,它连接到指定的WebService地址,设置SOAP版本和操作,发送请求并打印返回结果。
摘要由CSDN通过智能技术生成

axis2 天气预报 调用.net的webservice

webservice 2008-12-23 21:02:39 阅读315 评论0   字号: 订阅

package com.axisweather.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.axiom.soap.SOAP11Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
//  http://hi.baidu.com/e_ville/blog/item/f6529fa1a6616a8e471064a3.html
/**
* 测试WebService
* @author
* @history 2008-4-16
*/
public class ServiceGetWeather {
    private static EndpointReference targetEPR = new EndpointReference(
            "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");


    public void getResult()throws Exception{
        ServiceClient sender = new ServiceClient();
        sender.setOptions(buildOptions());
        OMElement result = sender.sendReceive(buildParam());
        System.out.println(result);
    }
    private static OMElement buildParam() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
        OMElement data = fac.createOMElement("getWeatherbyCityName", omNs);
        OMElement inner = fac.createOMElement("theCityName", omNs);
        inner.setText("北京");
        data.addChild(inner);
        return data;
    }

    private static Options buildOptions() {
        Options options = new Options();
        options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
        options.setAction("http://WebXml.com.cn/getWeatherbyCityName");
        options.setTo(targetEPR);
        //options.setProperty(propertyKey, property)
       
        // enabling MTOM in the client side
        // options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
        options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
        return options;
    }
    public static void main(String[] args) throws Exception {
     ServiceGetWeather s = new ServiceGetWeather();
     s.getResult();
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值