Java调webService服务

我是用Axis做的调用、

 

1所需jar

xmlsec-1.3.0.jar

axis.jar

axis-ant.jar

axis-schema-1.3.jar

axis-wsdl4j-1.5.1.jar

commons-discovery-0.2.jar

commons-logging-1.0.4.jar

jaxrpc.jar

log4j-1.2.8.jar

mailapi-1.3.jar

saaj.jar

wsdl4j-1.5.1.jar

这些jar包可能建项目的时候会有,自己查看一下没有的就去下载。

2建一个类、如下是java代码。

 

import java.io.IOException;

import java.net.MalformedURLException;

 

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import javax.xml.rpc.ServiceException;

import javax.xml.soap.SOAPException;

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.message.SOAPHeaderElement;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

/**

 *@authorxiaoqiang

 *

 *测试调用WebService

 */

publicclass CaService {

    privatestaticfinal Log log = LogFactory.getLog(CaService.class);

//这个是webService服务的地址

  privatestaticfinal String ENDPOINT = "http://localhost/as/Service.asmx";

    

    publicstaticvoid main(String[] args) {

    CaService tester = new CaService();

 

        tester.getServiceReturn();

   }

    publicvoid getServiceReturn() {

       

        try {

            Service service = new Service();

            Call call = (Call) service.createCall();

            call.setTargetEndpointAddress(new java.net.URL(ENDPOINT));

//此处为webService的方法          

 call.setOperationName(new QName("http://tempuri.org/","GetVehicle"));

//此处为方法的输入变量

            call.addParameter(new QName("http://tempuri.org/","VehicleNo"), org.apache.axis.Constants.XSD_STRING,

                    javax.xml.rpc.ParameterMode.IN);

            call.setReturnType(org.apache.axis.Constants.XSD_STRING);

            call.setUseSOAPAction(true);  

//soapAction地址

            call.setSOAPActionURI("http://tempuri.org/GetVehicle");

//传入weService值并返回的值ret

                String ret = (String)call.invoke(new Object[] {"ggggg"});

                System.out.println("The return value is:" + ret);

                 return;

       

        } catch (Exception e) {

            e.printStackTrace();

   

       }

        log.error("call service error!");

    }

 

 //java调用webService

    publicvoid getReturn3(){

         try {

 

              String endpoint = "http://localhost:8888/WS/services/TransService";

 

              //直接引用远程的wsdl文件

 

             //以下都是套路

              Service service = new Service();

 

              Call call = (Call) service.createCall();

 

              call.setTargetEndpointAddress(endpoint);

 

              call.setOperationName("getComp");//WSDL里面描述的接口名称

 

              call.addParameter("in0", org.apache.axis.encoding.XMLType.XSD_STRING,

 

                            javax.xml.rpc.ParameterMode.IN);//接口的参数

 

              call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 

 

 

              String temp = "测试人员";

 

              String result = (String)call.invoke(new Object[]{temp});

 

              //给方法传递参数,并且调用方法

 

              System.out.println("result is "+result);

 

       }

       catch (Exception e) {

              System.err.println(e.toString());

       }

     }

 

 

   }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值