使用AXIS调用WSDL描述的Web服务(续)

使用AXIS调用WSDL描述的Web服务(续)
今天发现自己陷入了一个误区。前面做的调用OWL-S服务时是使用描述文件链接进行调用的,所以对于远程的WSDL文件也一直用描述文件链接调用,今天和师兄讨论了一下,恍然大悟!WSDL和OWL-S不同,它调用时要使用 targetNamespace 来作为 TargetEndpointAddress 。而WSDL文件中其他的内容的作用是为了说明该服务有哪些接口、那些参数,以便调用的时候能够正确的进行参数的设置。OWL-S在调用的时候能够自动地获取操作的名称和参数类型,无需调用时指定;而WSDL文件则需要在调用之前指定操作和参数的信息(怪不得网上的调用的例子在调用之前都要首先对WSDL进行解析),否则会发生调用错误。也许这就是由于WSDL文件中可以包含多个操作,而OWL-S文件中只有一个操作的原因。下面同样是对 DictionaryService 进行调用的实例:
一、DictionaryService.wsdl 文件内容
<? xml version="1.0" encoding="UTF-8" ?>
< wsdl:definitions  targetNamespace ="http://www.mindswap.org/axis/services/DictionaryService"  xmlns:apachesoap ="http://xml.apache.org/xml-soap"  xmlns:impl ="http://www.mindswap.org/axis/services/DictionaryService"  xmlns:intf ="http://www.mindswap.org/axis/services/DictionaryService"  xmlns:soapenc ="http://schemas.xmlsoap.org/soap/encoding/"  xmlns:wsdl ="http://schemas.xmlsoap.org/wsdl/"  xmlns:wsdlsoap ="http://schemas.xmlsoap.org/wsdl/soap/"  xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
<!-- WSDL created by Apache Axis version: 1.2.1
Built on Jun 14, 2005 (09:15:57 EDT)
-->

   
< wsdl:message  name ="getMeaningRequest" >

      
< wsdl:part  name ="in0"  type ="xsd:string" />

   
</ wsdl:message >

   
< wsdl:message  name ="getMeaningResponse" >

      
< wsdl:part  name ="getMeaningReturn"  type ="xsd:string" />

   
</ wsdl:message >

   
< wsdl:portType  name ="DictionaryService" >

      
< wsdl:operation  name ="getMeaning"  parameterOrder ="in0" >

         
< wsdl:input  message ="impl:getMeaningRequest"  name ="getMeaningRequest" />

         
< wsdl:output  message ="impl:getMeaningResponse"  name ="getMeaningResponse" />

      
</ wsdl:operation >

   
</ wsdl:portType >

   
< wsdl:binding  name ="DictionaryServiceSoapBinding"  type ="impl:DictionaryService" >

      
< wsdlsoap:binding  style ="rpc"  transport ="http://schemas.xmlsoap.org/soap/http" />

      
< wsdl:operation  name ="getMeaning" >

         
< wsdlsoap:operation  soapAction ="" />

         
< wsdl:input  name ="getMeaningRequest" >

            
< wsdlsoap:body  encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/"  namespace ="http://services.mindswap.org"  use ="encoded" />

         
</ wsdl:input >

         
< wsdl:output  name ="getMeaningResponse" >

            
< wsdlsoap:body  encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/"  namespace ="http://www.mindswap.org/axis/services/DictionaryService"  use ="encoded" />

         
</ wsdl:output >

      
</ wsdl:operation >

   
</ wsdl:binding >

   
< wsdl:service  name ="DictionaryServiceService" >

      
< wsdl:port  binding ="impl:DictionaryServiceSoapBinding"  name ="DictionaryService" >

         
< wsdlsoap:address  location ="http://www.mindswap.org/axis/services/DictionaryService" />

      
</ wsdl:port >

   
</ wsdl:service >

</ wsdl:definitions >

二、调用过程代码
package  wsdl;

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

public   class  CallService  {

    
public static void main(String[] args) {

        
try {
                       
            String endpoint 
= "http://www.mindswap.org/axis/services/DictionaryService";
            
//调用过程
            Service service = new Service();
            
            Call call 
= (Call) service.createCall();
            
            call.setTargetEndpointAddress(
new  java.net.URL(endpoint));
            
            call.setOperationName(
"getMeaning");//WSDL里面描述的操作名称
            
            call.addParameter(
"getMeaningRequest", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);//操作的参数
            
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
//设置返回类型  
            
            call.setUseSOAPAction( 
true );
            
            
//给方法传递参数,并且调用方法
            String temp = "good";
            Object[] obj 
= new Object[]{temp};
            String result 
= (String)call.invoke(obj);
            
            System.out.println(
"Result is : "+result);
            }
 catch (Exception e) {
                e.printStackTrace();
            }

    }

}


你可能感兴趣的:(使用AXIS调用WSDL描述的Web服务(续))