使用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#异常类型

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...