使用Java客户端类调用C
创始人
2024-04-26 14:30:31
0次

使用这个类不用安装任何第三方工具,因为采用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#异常类型

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
粉嫩如何诠释霸道 东芝M805... “霸道粉”是个什么玩意东芝M805拿过来的时候,笔者扑哧笑了,不是笑这款笔记本,而是笑这款产品的颜色...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
30分钟搞定iOS自定义相机 最近公司的项目中用到了相机,由于不用系统的相机,UI给的相机切图,必须自定义才可以。就花时间简单研究...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...