axis2访问webservice

1.环境:axis2

2.pom.xml

<dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2</artifactId>
            <version>1.7.7</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.7.7</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.ws.commons.schema</groupId>
                    <artifactId>XmlSchema</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb-codegen</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.commons.axiom</groupId>
            <artifactId>axiom-api</artifactId>
            <version>1.2.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.commons.axiom</groupId>
            <artifactId>axiom-impl</artifactId>
            <version>1.2.20</version>
        </dependency>



        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>backport-util-concurrent</groupId>
            <artifactId>backport-util-concurrent</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.neethi</groupId>
            <artifactId>neethi</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-spring</artifactId>
            <version>1.7.7</version>
        </dependency>
        
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

3.代码

public static void main(String[] args) throws AxisFault {
        String endpoint = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
        String targetNamespace = "http://WebXml.com.cn/";
        //所调用接口的方法method  
        String methodStr = "getWeather";

        EndpointReference targetEPR = new EndpointReference(endpoint);
        Options options = new Options();
        String namespace = targetNamespace;

        options.setSoapVersionURI(SOAP11Constants.SOAP_ENCODING_NAMESPACE_URI);
        // 调用接口方法

        options.setAction(namespace + methodStr);

        options.setTo(targetEPR);
        options.setProperty(HTTPConstants.CHUNKED, "false");//设置不受限制.
        options.setCallTransportCleanup(Boolean.TRUE);
        options.setTimeOutInMilliSeconds(10 * 1000);//超时时间定为1分钟
        ServiceClient sender = null;
        try {
            sender = new ServiceClient();
            sender.setOptions(options);

            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace omNs = fac.createOMNamespace(namespace, "");
            OMElement method = fac.createOMElement(methodStr, omNs);

            OMElement param = fac.createOMElement("theCityCode", omNs);
            param.setText("北京");
            method.addChild(param);

            method.build();
            OMElement response = sender.sendReceive(method);
            System.out.println(response);

        } catch (AxisFault e) {
            e.printStackTrace();
        }

    }

4.访问结果

<getWeatherResponse xmlns="http://WebXml.com.cn/"><getWeatherResult><string>直辖市 北京</string><string>北京</string>。。。。。。
/getWeatherResponse>

猜你喜欢

转载自www.cnblogs.com/malaya/p/9138703.html