Java调用webservice接口方法

t org.apache.axis.client.Service;

 

 

public class LogonClientWithURL {

  public static void main(String args[]) throws Exception {

        try {

                  String urlname = "http://192.168.194.23:9080/Logon/services/Logon?wsdl" ;

                  urlname = "http://192.168.194.23:9080/Logon/services/Logon";

                   

                  Service s = new  Service();

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

                  call.setTimeout(new Integer(5000));

                  call.setOperation( "getSecurityToken" );

                  call.setTargetEndpointAddress(urlname);

                     

                  Object[] fn01 = { "john" , "john" , null ,null };

                  String val = (String)call.invoke(fn01);

                  System.out .println( "getSecurityToken(correct):"  + val);

                   

                  Object[] fn02 = { "john" , "john2" , null ,null };

                  String va2 = (String)call.invoke(fn02);

                  System.out .println( "getSecurityToken(wrong):"  + va2);

           

        } catch (Exception e) {

              //java.io.InterruptedIOException: Read timed out

              System.out.println(e.getMessage());

        }

 

  }

}

 

1.2 [Apache提供]使用WSDL2Java把WSDL文件转成本地类,然后像本地类一样使用。

这种方式应该可以调用所有的webService。

同时这个调用方式适合那种业务比较复杂的Service (特别是企业应用, 不是外部所有人都能无限制访问那种Service),比如,这个Service返回的xml内容比较复杂,同时多个客户端系统都需要访问这个Service,在这种情况,service提供方可能会考虑返回一个java类。毕竟,每个客户端都要解析xml也够麻烦的,还不如让service提供方返回一个类。

 

调用模式如下:

1)使用WSDL2Java把WSDL文件转成本地类。 我这里写了bat文件:

<<WSDL2JAVA.bat>>

set Axis_Lib=.\lib

set Java_Cmd=D:\Dev\JDK\jdk1.4.2_12\bin\java -Djava.ext.dirs=%Axis_Lib%

set Output_Path=.\src

set Package=com.ubs.ws

%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% http:\\192.168.194.23:9080\Logon\services\Logon\wsdl\Logon.wsdl

Pause

 

运行直接生成以下java类

Logon.java

LogonService.java

LogonServiceLocator.java

LogonSoapBindingStub.java

 

2) <<LogonClientWithStub.java>>

package test.cis.client;

 

import test.cis.*;

 

public class LogonClientWithStub {

 

      public static void main(String[] args) {

            try {

 

                  Logon locator = new LogonServiceLocator().getLogon();

                  String result = locator.getSecurityToken("john" , "john" ,null , null );

 

                  System.out.println("getSecurityToken(correct): " + result);

                 

                 

                  result = locator.getSecurityToken("john" , "john2" ,null , null );

 

                  System.out.println("getSecurityToken(wrong): " + result);

                 

            } catch (Exception e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

            }                      

           

      }

 

}

 

1.3 [Apache提供]直接SOAP调用远程的webservice

apache的soap项目,不过如今已经被axis取代了,后者重新进行了架构设计,功能更齐全。

前者已停止开发。所以建议用axis。也就是1.1 & 1.3

 

1.4 [XFire提供] 使用XFire调用WebService

有兴趣的可以下载XFire的jar来学习一下。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值