• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 

[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]


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#实现文本文件合并发布时间:2022-07-10
下一篇:
C# Access2003/2007 连接字符串总结发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap