微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实现PRC调用WebService的客户端程序

    WebService是为程序服务的,只在浏览器中访问WebService是没有意义的。因此,在本节使用Java实现了一个控制台程序来调用上一节发布的WebService调用WebService的客户端代码如下:

package  client;

import  javax.xml.namespace.QName;
import  org.apache.axis2.addressing.EndpointReference;
import  org.apache.axis2.client.Options;
import  org.apache.axis2.rpc.client.RPCServiceClient;

public   class  RPcclient
{
    
public   static   void  main(String[] args)  throws  Exception  
    {
        
//   使用RPC方式调用WebService        
        RPCServiceClient serviceClient  =   new  RPCServiceClient();
        Options options 
=  serviceClient.getoptions();
        
//   指定调用WebService的URL
        EndpointReference targetEPR  =   new  EndpointReference(
                
" http://localhost:8080/axis2/services/SimpleService " );
        options.setTo(targetEPR);
        
//   指定getGreeting方法的参数值
        Object[] opAddEntryArgs  =   new  Object[] { " 超人 " };
        
//   指定getGreeting方法返回值的数据类型的Class对象
        Class[] classes  =   new  Class[] {String. class };
        
//   指定要调用的getGreeting方法及WSDL文件的命名空间
        QName opAddEntry  =   new  QName( " http://ws.apache.org/axis2 " " getGreeting " );
        
//   调用getGreeting方法输出方法的返回值
        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[ 0 ]);
        
//   下面是调用getPrice方法代码,这些代码调用getGreeting方法代码类似
        classes  =   new  Class[] { int . class };
        opAddEntry 
=   new  QName( " http://ws.apache.org/axis2 " " getPrice " );
        System.out.println(serviceClient.invokeBlocking(opAddEntry, 
new  Object[]{}, classes)[ 0 ]);
    } 
}

 

 

 

 

运行上面的程序后,将在控制台输出如下的信息:

 

你好 超人
443

    在编写客户端代码时应注意如下几点:

    1. 客户端代码需要引用很多Axis2jar包,如果读者不太清楚要引用哪个jar包,可以在Eclipse的工程中引用Axis2发行包的lib目录中的所有jar包。

    2. 在本例中使用了RPCServiceClient类的invokeBlocking方法调用WebService中的方法invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用方法名;第二个参数表示要调用WebService方法的参数值,参数类型为Object[];第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}

    3. 如果被调用WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。

    4. 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:deFinitions>元素的targetNamespace属性值,下面是SimpleService生成WSDL文件代码片段:

<? xml version="1.0" encoding="UTF-8" ?>
< wsdl:deFinitions  xmlns:wsdl ="http://schemas.xmlsoap.org/wsdl/"  xmlns:ns1 ="http://org.apache.axis2/xsd"
xmlns:ns
="http://ws.apache.org/axis2"  xmlns:wsaw ="http://www.w3.org/2006/05/addressing/wsdl"
xmlns:http
="http://schemas.xmlsoap.org/wsdl/http/"  xmlns:xs ="http://www.w3.org/2001/XMLSchema"
xmlns:mime
="http://schemas.xmlsoap.org/wsdl/mime/"  xmlns:soap ="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12
="http://schemas.xmlsoap.org/wsdl/soap12/"
targetNamespace
="http://ws.apache.org/axis2"
>
    
< wsdl:types >
        

 


    
</ wsdl:types >
    

 


</ wsdl:deFinitions >

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐