调用WebService 小例子

//一:使用XFire框架
import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.xfire.client.Client;


public class Test {

	/**
	 * @param args
	 * @throws Exception 
	 * @throws MalformedURLException 
	 */
	public static void main(String[] args) throws MalformedURLException, Exception {
        Client client = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));  
        Object[] results = client.invoke("qqCheckOnline", new String[] { "35***272" });  
        System.out.println(results[0]);
	}

}


二:通过axis2调用远程Web Service,实现天气预报功能(转)

准备工作:

1.下载axis2-1.4.1的bin包,地址:http://apache.etoak.com/ws/axis2/1_5/axis2-1.5-bin.zip

2.解压,将lib目录下的所有的jar包拷贝出来,在这里建议大家在自己的电脑上专门建立一个文件夹放jar包,比如:jdbc的jar包,Struts的jar包,spring的jar包等等,这里,我们将axis2的jar包专门拷贝出来放在一个目录下。

 

接下来的步骤是:

1、new java project 

2、引入刚才axis2的所有jar包

3、new class

参考代码如下:

package com.weather.webservice;

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;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Test
{
    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 如果不是通过代理上网,此句可省
        //options.setProperty(HTTPConstants.PROXY, buildProxy());
        options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
        return options;
    }

    /**
     * 本机采用代理服务器上网时,需要设置代理
     * @return
     */
    public static ProxyProperties buildProxy()
    {
        ProxyProperties proxyProperties = new ProxyProperties();
        proxyProperties.setProxyName("代理名称");
        proxyProperties.setProxyPort(8080);
        return proxyProperties;
    }

    public static void main(String[] args) throws Exception
    {
        Test s = new Test();
        s.getResult();
    }

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值