当前位置: 代码迷 >> Web前端 >> Axis2调用网下免费的WebService服务过程
  详细解决方案

Axis2调用网下免费的WebService服务过程

热度:137   发布时间:2012-09-12 09:21:30.0
Axis2调用网上免费的WebService服务过程
步骤一:下载Axis2,http://axis.apache.org/axis2/java/core/download.cgi,选择Binary Distribution zip

步骤二:新建一个java工程,并把下载的Axis2中的lib里的jar包都引入进来

步骤三:新建class代码如下:
package cn.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;
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 TestZip
{
    private static EndpointReference targetEPR = new EndpointReference(
            "http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.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("getAddressByZipCode", omNs);
        OMElement inner = fac.createOMElement("theZipCode", omNs);
        OMElement inner1 = fac.createOMElement("userID", omNs);
        inner.setText("425900");
        inner1.setText("");
        data.addChild(inner);
        data.addChild(inner1);
        return data;
    }

    private static Options buildOptions()
    {
        Options options = new Options();
        options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
        options.setAction("http://WebXml.com.cn/getAddressByZipCode");
        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
    {
        TestZip s = new TestZip();
        s.getResult();
    }

}

输出是:<getAddressByZipCodeResponse xmlns="http://WebXml.com.cn/"><getAddressByZipCodeResult><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="ZipInfo"><xs:complexType><xs:sequence><xs:element name="PROVINCE" type="xs:string" minOccurs="0" /><xs:element name="CITY" type="xs:string" minOccurs="0" /><xs:element name="ADDRESS" type="xs:string" minOccurs="0" /><xs:element name="ZIP" type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><NewDataSet xmlns=""><ZipInfo diffgr:id="ZipInfo1" msdata:rowOrder="0"><PROVINCE>湖南</PROVINCE><CITY>东安县</CITY><ADDRESS>白牙市镇</ADDRESS><ZIP>425900</ZIP></ZipInfo><ZipInfo diffgr:id="ZipInfo2" msdata:rowOrder="1"><PROVINCE>湖南</PROVINCE><CITY>东安县</CITY><ADDRESS>茶源乡</ADDRESS><ZIP>425900</ZIP></ZipInfo><ZipInfo diffgr:id="ZipInfo3" msdata:rowOrder="2"><PROVINCE>湖南</PROVINCE><CITY>湖南</CITY><ADDRESS>东安县</ADDRESS><ZIP>425900</ZIP></ZipInfo><ZipInfo diffgr:id="ZipInfo4" msdata:rowOrder="3"><PROVINCE>湖南</PROVINCE><CITY>东安县</CITY><ADDRESS>荷池乡</ADDRESS><ZIP>425900</ZIP></ZipInfo><ZipInfo diffgr:id="ZipInfo5" msdata:rowOrder="4"><PROVINCE>湖南</PROVINCE><CITY>东安县</CITY><ADDRESS>水岭乡</ADDRESS><ZIP>425900</ZIP></ZipInfo><ZipInfo diffgr:id="ZipInfo6" msdata:rowOrder="5"><PROVINCE>湖南</PROVINCE><CITY>东安县</CITY><ADDRESS>县城(白牙市镇)</ADDRESS><ZIP>425900</ZIP></ZipInfo></NewDataSet></diffgr:diffgram></getAddressByZipCodeResult></getAddressByZipCodeResponse>
  相关解决方案