zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    常用Docker命令
    Ubuntu16.04安裝最新Nvidia驱动
    (转)5分钟让你明白“软链接”和“硬链接”的区别
    Centos7 Zabbix3.2集群安装
    Kettle定时抽取两个库中的两个表到目标库SYS_OPLOG表
    Elasticsearch Java API—多条件查询(must)
    logstash采集tomcat日志、mysql错误日志
    让Logstash每次都从头读文件及常见问题
    logstash之multiline插件,匹配多行日志
    spring security积累
  • 原文地址:https://www.cnblogs.com/malaya/p/9138703.html
Copyright © 2011-2022 走看看