使用Java客户端类调用C# WebService

开发 后端
本文介绍一个非常实用的Java客户端工具类来调用C# WebServices和apache xml rpc server,顺便在这里也给大家介绍一下C#如何处理此类发送的xml数据。

使用这个类不用安装任何第三方工具,因为采用http的方式发送xml文件,所以你只需要安装好JDK就可以了。执行此类还可以获得WebServices或xml rpc server返回的xml字符流,你可以根据返回的xml数据来进行其他程序处理。通过这种方式实现了Java平台和.NET平台的数据交换和Java客户端类调用C# WebService。

下面是满足Java客户端类调用的源代码SOAPClient4XG.java:

  1. /**   
  2. * SOAPClient4XG. Read the SOAP envelope   
  3. file passed as the second 
  4. * parameter, pass it to the SOAP endpoint   
  5. passed as the first parameter, and   
  6.  
  7. * print out the SOAP envelope passed   
  8. as a response. with help from Michael  
  9. * Brennan 03/09/01  
  10. *   
  11. *  
  12. * @author Bob DuCharme  
  13. * @version 1.1  
  14. * @param SOAPUrl URL of SOAP Endpoint   
  15. to send request.  
  16. * @param xmlFile2Send A file with an XML   
  17. document of the request.   
  18. *  
  19. * 5/23/01 revision: SOAPAction added  
  20. */  
  21.  
  22. import java.io.*;  
  23. import java.net.*;  
  24.  
  25. public class SOAPClient4XG {  
  26. public static void main(String[] args)   
  27. throws Exception {  
  28.  
  29. if (args.length < 2) { //小于  
  30. System.err.println("Usage: java SOAPClient4XG " +  
  31. "http://soapURL soapEnvelopefile.xml" +  
  32. " [SOAPAction]");  
  33. System.err.println("SOAPAction is optional.");  
  34. System.exit(1);  
  35. }  
  36.  
  37. String SOAPUrl = args[0];   
  38. String xmlFile2Send = args[1];  
  39.  
  40. String SOAPAction = "";  
  41. if (args.length > 2) //大于  
  42. SOAPAction = args[2];  
  43.  
  44. // Create the connection where we're going   
  45. to send the file.  
  46. URL url = new URL(SOAPUrl);  
  47. URLConnection connection =   
  48. url.openConnection();  
  49. HttpURLConnection httpConn =   
  50. (HttpURLConnection) connection;  
  51.  
  52. // Open the input file. After we copy   
  53. it to a byte array, we can see  
  54. // how big it is so that we can set the   
  55. HTTP Cotent-Length  
  56. // property. (See complete e-mail below   
  57. for more on this.)  
  58.  
  59. FileInputStream fin =   
  60. new FileInputStream(xmlFile2Send);  
  61.  
  62. ByteArrayOutputStream bout =   
  63. new ByteArrayOutputStream();  
  64.  
  65. // Copy the SOAP file to the open connection.  
  66. copy(fin,bout);   
  67. fin.close();  
  68.  
  69. byte[] b = bout.toByteArray();  
  70.  
  71. // Set the appropriate HTTP parameters.  
  72. httpConn.setRequestProperty( "Content-Length",  
  73. String.valueOf( b.length ) );  
  74. httpConn.setRequestProperty("Content-Type","  
  75. text/xml; charset=utf-8");  
  76. httpConn.setRequestProperty("SOAPAction",SOAPAction);  
  77. httpConn.setRequestMethod( "POST" );  
  78. httpConn.setDoOutput(true);  
  79. httpConn.setDoInput(true);  
  80.  
  81. // Everything's set up; send the XML   
  82. that was read in to b.  
  83. OutputStream out = httpConn.getOutputStream();  
  84. out.write( b );   
  85. out.close();  
  86.  
  87. // Read the response and write it   
  88. to standard out.  
  89.  
  90. InputStreamReader isr =  
  91. new InputStreamReader(httpConn.getInputStream());  
  92. BufferedReader in = new BufferedReader(isr);   
  93.  
  94. String inputLine;  
  95.  
  96. while ((inputLine = in.readLine()) != null)  
  97. System.out.println(inputLine);  
  98. in.close();  
  99. }  
  100.  
  101. // copy method from From E.R. Harold's   
  102. book "Java I/O" 
  103. public static void copy(InputStream in,   
  104. OutputStream out)   
  105. throws IOException {  
  106.  
  107. // do not allow other threads to read from the  
  108. // input or write to the output while copying is 
  109. // taking place  
  110.  
  111. synchronized (in) {  
  112. synchronized (out) {  
  113.  
  114. byte[] buffer = new byte[256];  
  115. while (true) {  
  116. int bytesRead = in.read(buffer);  
  117. if (bytesRead == -1) break;  
  118. out.write(buffer, 0, bytesRead);  
  119. }  
  120. }  
  121. }  
  122. }   
  123. }  

编译:javac SOAPClient4XG.java

运行的命令格式: java -classpath . SOAPClient4XG

http://localhost/BokeServices/Service1.asmx c:loginReq.xml

http://tempuri.org/UserLoginReq

不过先不要运行上面的命令,先介绍一下命令行的意思,http://localhost/BokeServices/Service1.asmx是C# WebService的地址;

c:loginReq..xml里的内容是Java客户端类调用WebService方法的xml文件, http://tempuri.org是WebService方法的命名空间,一定要有,否则调用失败,如果你在C# WebServices中使用了方法默认的命名空间的话,就使用http://tempuri.org,否则要与C#中定义的一致,UserLoginReq是C# WebServices的方法名。注意xml文件中的方法名和参数名是与C# WebServices的方法名、参数名是一一对应的(参数顺序是可以颠倒的)。

我先介绍一个简单的例子(c:loginReq.xml),这个xml文件调用了远程C# WebService的UserLoginReq方法,并带UserAcc(用户名)和UserPwd(口令)两个参数,调用成功后C#会自动返回一个xml格式的SOAP包。

  1. 〈?xml version="1.0" encoding="utf-8"?〉  
  2. 〈soap:Envelope xmlns:xsi="  
  3. http://www.w3.org/2001/XMLSchema-instance"   
  4. xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
  5. xmlns:soap="http://schemas.xmlsoap.org/soap/  
  6. envelope/"〉  
  7. 〈soap:Body〉  
  8. 〈UserLoginReq xmlns="http://tempuri.org/"〉  
  9. 〈UserAcc〉baozheng〈/UserAcc〉  
  10. 〈UserPwd〉mypwd〈/UserPwd〉  
  11.  
  12. 〈/UserLoginReq〉  
  13. 〈/soap:Body〉  
  14. 〈/soap:Envelope〉 

现在看一下C# WebServices的UserLoginReq的方法的定义:

  1. public struct UserLoginResp  
  2. {  
  3. public string UserAcc;  
  4. public int Result;  
  5. }  
  6. [WebMethod]   
  7. public UserLoginResp UserLoginReq(string UserAcc,  
  8. string UserPwd,int ReqFrom)  
  9. {  
  10. …  
  11. }  

注意结构UserLoginResp,C# WebServices返回SOAP信息时会自动将UserLoginResp结构转换成xml的格式。

用此类做xml rpc server 的客户端也很简单,下文是一个客户端rpc.xml文件,调用了xml rpc server 端实现的metaWeblog.deletePost方法。

  1. 〈?xml version="1.0" encoding="utf-8"?〉  
  2. 〈methodCall〉  
  3. 〈methodName〉metaWeblog.deletePost〈/methodName〉  
  4. 〈params〉  
  5. 〈param〉〈value〉appKeyValue〈/value〉〈/param〉  
  6. 〈param〉〈value〉746〈/value〉〈/param〉  
  7.  
  8. 〈param〉〈value〉baozheng〈/value〉〈/param〉  
  9. 〈param〉〈value〉Hello123〈/value〉〈/param〉  
  10. 〈/params〉   
  11.  
  12. 〈/methodCall〉  

调用命令的格式:

java -classpath %CLASSPATH%;. SOAPClient4XG.

http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc rpc.xml

对比调用C# WebServices的命令行,可以看出调用xml rpc server的命令行少一个方法名参数。http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc 是提供xml rpc 调用的server端servlet地址。

【编辑推荐】

  1. C# WebService发布与调用浅析
  2. 简明教程 C# Webservice实例
  3. C#中定义装箱和拆箱详解
  4. 浅谈C#类型系统
  5. 三种不同的C#异常类型
责任编辑:冰荷 来源: it55
相关推荐

2009-08-06 17:12:13

C# WebServi

2020-03-19 08:00:00

客户端KubernetesAPI

2009-08-21 16:14:52

服务端与客户端通信

2009-08-21 15:59:22

服务端与客户端通信

2009-08-21 15:36:41

服务端与客户端

2009-08-21 15:54:40

服务端与客户端

2009-08-11 14:26:56

C#动态调用WebSe

2015-07-09 10:44:48

C#WebService

2009-08-21 17:48:43

C#网络编程

2009-08-21 17:53:25

C#网络编程客户端程序

2009-08-06 17:57:14

C# webServiC# WebServi

2011-08-17 10:10:59

2011-08-25 10:37:15

leveldb的访问封C#客户端源码

2009-08-06 16:44:03

C#创建WebServ

2009-08-21 14:33:15

C#异步传输字符串

2012-10-11 17:02:02

IBMdw

2010-05-12 15:46:51

Subversion客

2011-04-07 09:33:01

Activex

2011-03-21 14:53:36

Nagios监控Linux

2011-04-06 14:24:20

Nagios监控Linux
点赞
收藏

51CTO技术栈公众号