axis1.x调webservice程序

阅读更多

 

   最近在项目中遇到需要去调webservice的接口,我以前很少写,这里写一个例子,分享出来!没太大技术含量,不喜勿喷!!!

 1.pom.xml添加依赖包

   



    4.0.0

    com.xiaoyetanmodules
    webserviceClient
    1.0
    war

    webserviceClient
    http://maven.apache.org

    
        UTF-8
        1.8
    

    
        
        
            jfinal
            jfinal
            2.0
            system
            ${basedir}/src/main/webapp/WEB-INF/lib/jfinal-2.0-bin-with-src.jar
        
        
            com.jfinal
            cos
            26Dec2008
        
        
            javax.servlet
            servlet-api
            2.3
            provided
        

        
        
            org.apache.axis
            axis
            1.4
        
        
            commons-discovery
            commons-discovery
            0.2
        
        
            axis
            axis-jaxrpc
            1.2
        
        
            axis
            axis-wsdl4j
            1.2
        
        
            javax.mail
            mail
            1.4
        
    

    
        ${project.basedir}/src/main/webapp/WEB-INF/classes/
        
            
                ${project.basedir}/src/main/resources
                true
            
        
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    ${jdk.version}
                    ${jdk.version}
                    true
                
            

            
            
                org.apache.maven.plugins
                maven-war-plugin
                
                
            

            
            
                org.apache.maven.plugins
                maven-resources-plugin
                
                    ${project.build.sourceEncoding}
                
            

            
            
                org.apache.maven.plugins
                maven-install-plugin
            

            
            
                org.apache.maven.plugins
                maven-clean-plugin
            

            
            
                org.apache.maven.plugins
                maven-antrun-plugin
            

            
    
    
        
            nexus-aliyun
            Nexus aliyun
            http://maven.aliyun.com/nexus/content/groups/public
        
    


 

2.core code

  

package com.xiaoyetan;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import java.net.URL;

/**
 * @Author xiaoyetan
 * @Date :created on 13:18 2017/8/24
 */
public class AxisClient {
    public static void main(String[] args) {
        String url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl" ;
        //创建客户端调用webservice的代理对象
        Service service = new Service();
        try {
            //创建一个调用对象,代表对web service 的一次调用
            Call call = (Call) service.createCall();
            //设置web service的url 地址
            call.setTargetEndpointAddress(new java.net.URL(url));
            //设置操作名称,QName 对象的两个参数分别为命名空间和方法名称
            call.setOperationName(new QName("http://WebXml.com.cn/","qqCheckOnline"));
            //不加这行会抛异常System.Web.Services.Protocols.SoapException: 服务器未能识别 HTTP 头 SOAPAction 的值:
            call.setSOAPActionURI("http://WebXml.com.cn/qqCheckOnline");
            //传参
            call.addParameter(new QName("http://WebXml.com.cn/", "qqCode"), XMLType.XSD_STRING, ParameterMode.IN);
            //设置返回值类型                                                         
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
            // 执行调用操作,result 保存返回的结果,invoke 的参数为实参
            String result = (String) call.invoke(new Object[]{"1160500991"});
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

 

就这么愉快的结束了!!!很简单 

  • webserviceClient.zip (811.5 KB)
  • 下载次数: 0

你可能感兴趣的:(axis1.x,webservice,client)