[Java] 利用Axis库调用C#的Web Service

 

[Java] 利用Axis库调用C#Web Service

编写者

日期

关键词

郑昀@ultrapower

2005-8-2

Java Web Service Axis C#

 

概述

试图从Java调用C#编写的Web Service,借用了王咏刚的wsCaller源代码中DynamicInvoker类。

开始不清楚DynamicInvoker类的portName的含义,望文生义,以为是8080之类的端口号,实际上是下面wsdl中的wsdl:port 节点的“name”属性值“ReadWordSoap”:

wsdl提示:

<wsdl:service name="ReadWord">

  <documentation xmlns="http://schemas.xmlsoap.org/wsdl/">提供Office文档的有选择性地读取文本服务.</documentation>

<wsdl:port name="ReadWordSoap" binding="tns:ReadWordSoap">

  <soap:address location="http://192.168.1.113/ReadOffice/ReadWord.asmx" />

  </wsdl:port>

  </wsdl:service>

 

需要特别引入的7jar包如下所示:

Jar

对应的库

用途

Axis.jar

org.apache.axis.*

 

wsdl4j-1.5.1.jar

javax.wsdl.*

wsdl操作

jaxrpc.jar

javax.xml.rpc.*

xml.rpc

commons-discovery-0.2.jar

 

discovery

log4j-1.2.11.jar

 

Axis需要log4net

commons-logging.jar

 

Axis需要LogFactory

saaj.jar

 

引入SOAP

 

演示代码

请求一个C#编写的Web Service,利用DynamicInvoker遍历web service每一个方法,获取serverName、参数列表以及类型

代码演示:

import java.util.Map;

import java.util.Vector;

import java.util.Iterator;

import java.util.Arrays;

 

import org.apache.axis.wsdl.symbolTable.*;

 

public class wsCaller {

 

      /**

       * @param args

       */

      public static void main(String[] args) {

          

           String wsdlLocation =

            "http://192.168.1.113/ReadOffice/ReadWord.asmx?wsdl";

           String serviceName = "ReadWord";

        String operationName = "read";

        String strFirstParam = "c:\\nlb.log";

           String strSecondParam = "0";

           String strThirdParam = "200";

       

        String portName = "ReadWordSoap";

 

           /*

            * 添加web service方法的输入参数到Vector

            */

           Vector parameterValues = new Vector();

           parameterValues.addElement(strFirstParam);

           parameterValues.addElement(strSecondParam);

           parameterValues.addElement(strThirdParam);

          

           try

           {

              DynamicInvoker invoker = new DynamicInvoker(wsdlLocation);

                

                 /*

                  * 遍历web service每一个方法,获取serverName、参数列表以及类型的过程

                  */

                 Vector vServices = invoker.enumServiceNames();

                 Object[] Services = vServices.toArray();

                 Arrays.sort(Services);

                 for (int i = 0; i < Services.length; i++)

                 {

                      String strServiceName = (String) Services[i];

                      System.out.println("ServiceName=" + serviceName);

                                            

                      Vector V = invoker.enumPortNames(strServiceName);

                      String strPortName = (String) V.elementAt(0);

                      System.out.println("PortName=" + strPortName);

                     

                      V = invoker.enumOperationNames(strServiceName, strPortName);

                      Object[] OperationNames = V.toArray();

                      Arrays.sort(OperationNames);

                      for (int j = 0; j < OperationNames.length; j++)

                     {

                            String strOperationName = (String) OperationNames[j];

                            System.out.println("\t" + (String) OperationNames[j]);

                            Parameters parameters = invoker.enumParameters(strServiceName, strPortName, strOperationName);

                            Vector v = parameters.list;

                            Vector vParameterValues = new Vector();

                            for (int k = 0; k < v.size(); k++)

                            {

                                  Parameter para = (Parameter) v.elementAt(k);

                                  System.out.print("\t\t" + para.getQName().getLocalPart());

                                  if (para.getType().getQName().getLocalPart().equals("string"))

                                  {

                                       vParameterValues.addElement("asdasdas");

                                  }

                                 else

                                 {

                                        vParameterValues.addElement(null);

                                 }

                                 System.out.print("\t" + para.getType().getQName().getLocalPart());

                                 System.out.println("\t" + invoker.getParameterModeString(para));

                            }

                    }

                 }

                  

// 最后一个参数是调用web service的超时时间,这个原来是固化在类里面的

// 但其实应该暴露出来

              Map map = invoker.invoke(serviceName,

                            portName, operationName, parameterValues,

                            new Integer(60 * 1000));

     

              // print result

              System.out.println("Result:");

              for (Iterator it = map.keySet().iterator(); it.hasNext();)

                 {

                  String name = (String) it.next();

                      System.out.println(name + "=" + map.get(name));

              }

              System.out.println("\nDone!");

           }

           catch (Exception exc) {

                 exc.printStackTrace();

        }

      }

 

}

 

最后输出的结果如下所示:

ServiceName=ReadWord

PortName=ReadWordSoap

    read

        >read>DocumentFilePath  string  [IN]

        >read>begin int [IN]

        >read>end   int [IN]

Result:

>readResponse>readResult=0018

 

立此存照。

 

编写者

日期

关键词

郑昀@ultrapower

2005-8-2

Java Web Service Axis C#

 

posted @ 2005-08-02 11:24  老兵笔记  阅读(2967)  评论(2编辑  收藏  举报