通过所给的xml文件来远程调用service服务方法

来源:互联网 发布:爱知日语培训班价格 编辑:程序博客网 时间:2024/05/17 21:39

所给的XML文档

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://localhost:8081/axis/services/testaxis" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8081/axis/services/testaxis" xmlns:intf="http://localhost:8081/axis/services/testaxis" 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.4
Built on Apr 22, 2006 (06:55:48 PDT)-->

   <wsdl:message name="getUserResponse">

      <wsdl:part name="getUserReturn" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="getUserRequest">

      <wsdl:part name="name" type="soapenc:string"/>

   </wsdl:message>

   <wsdl:message name="authenticateResponse">

      <wsdl:part name="authenticateReturn" type="xsd:dateTime"/>

   </wsdl:message>

   <wsdl:message name="authenticateRequest">

   </wsdl:message>

   <wsdl:portType name="TestAxis">

      <wsdl:operation name="authenticate">

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

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

      </wsdl:operation>

      <wsdl:operation name="getUser" parameterOrder="name">

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

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

      </wsdl:operation>

   </wsdl:portType>

   <wsdl:binding name="testaxisSoapBinding" type="impl:TestAxis">

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

      <wsdl:operation name="authenticate">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="authenticateRequest">

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

         </wsdl:input>

         <wsdl:output name="authenticateResponse">

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

         </wsdl:output>

      </wsdl:operation>

      <wsdl:operation name="getUser">

         <wsdlsoap:operation soapAction=""/>

         <wsdl:input name="getUserRequest">

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

         </wsdl:input>

         <wsdl:output name="getUserResponse">

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

         </wsdl:output>

      </wsdl:operation>

   </wsdl:binding>

   <wsdl:service name="TestAxisService">

      <wsdl:port binding="impl:testaxisSoapBinding" name="testaxis">

         <wsdlsoap:address location="http://192.168.4.55:8081/axis/services/testaxis"/>

      </wsdl:port>

   </wsdl:service>

</wsdl:definitions>

方法的调用

package com.yysoft.chen;

import java.rmi.RemoteException;
import java.util.Date;

import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;

import org.apache.tools.ant.types.resolver.ApacheCatalog;
public class TextService {
 public static void main(String[] args) {
  testaxiss();
 }
 
 public static void testaxis(){ 
  try {
   QName servicename=new QName("http://192.168.4.55:8081/axis/services/testaxis","TestAxisService");
   Service service=ServiceFactory.newInstance().createService(servicename);
   Call call=service.createCall();
   call.setTargetEndpointAddress("http://192.168.4.55:8081/axis/services/testaxis?wsdl");
   QName operationname=new QName("http://localhost:8081/axis/services/testaxis","authenticate");
   call.setOperationName(operationname);
   QName returnname=new QName("http://www.w3.org/2001/XMLSchema","dateTime");
   call.setReturnType(returnname, java.util.Date.class);
   Date d= (Date) call.invoke(new Object[]{});
   System.out.println(d.toLocaleString());
   
  } catch (ServiceException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void testaxiss(){
  try {
   QName servicename=new QName("http://192.168.4.55:8081/axis/services/testaxis","TestAxisService");
   Service service=ServiceFactory.newInstance().createService(servicename);
   Call call=service.createCall();
   call.setTargetEndpointAddress("http://192.168.4.55:8081/axis/services/testaxis?wsdl");
   
   QName operationname=new QName("http://localhost:8081/axis/services/testaxis","getUser");
   call.setOperationName(operationname);
   
   QName namename=new QName("http://schemas.xmlsoap.org/soap/encoding/","name");
   call.addParameter("name", namename, javax.xml.rpc.ParameterMode.IN);
   
   QName returnname=new QName("http://schemas.xmlsoap.org/soap/encoding/","string");
   call.setReturnType(returnname, String.class);  //将设好的返回类型装载入call中
   String version=(String)call.invoke(new Object[]{"fffsad"});
   System.out.println(version);
   
  } catch (ServiceException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void version(){
  try {
   //Qname("名称空间","对应名称");
   
   //设置服务名及对应的名称空间
   QName servicename=new QName("http://localhost:8081/axis/services/Version","VersionService");
   //根据对应的名称空间和服务名称创建一个服务
   Service service=ServiceFactory.newInstance().createService(servicename);
   //通过service创建一个call(调用方法)
   Call call=service.createCall();
   call.setTargetEndpointAddress("http://192.168.4.55:8081/axis/services/Version?wsdl");
   
   //设置方法名及对应的名称空间
   QName operationname=new QName("http://localhost:8081/axis/services/Version","getVersion");
   call.setOperationName(operationname); //将设好的方法装载入call中
   //设置返回类型及对应的名称空间
   QName returnname=new QName("http://www.w3.org/2001/XMLSchema","string");
   call.setReturnType(returnname, String.class);  //将设好的返回类型装载入call中
   String version=(String)call.invoke(new Object[]{});
   System.out.println(version);
   
  } catch (ServiceException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

原创粉丝点击