成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用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 15:59:22

服務端與客戶端通信

2009-08-21 16:14:52

服務端與客戶端通信

2009-08-21 15:36:41

服務端與客戶端

2009-08-21 15:54:40

服務端與客戶端

2009-08-06 17:57:14

C# webServiC# WebServi

2009-08-11 14:26:56

C#動態調用WebSe

2015-07-09 10:44:48

C#WebService

2009-08-06 16:44:03

C#創建WebServ

2011-08-17 10:10:59

2009-08-21 17:48:43

C#網絡編程

2009-08-21 17:53:25

C#網絡編程客戶端程序

2011-04-07 09:33:01

Activex

2012-10-11 17:02:02

IBMdw

2010-05-12 15:46:51

Subversion客

2011-08-25 10:37:15

leveldb的訪問封C#客戶端源碼

2024-12-23 06:00:00

TCPC#網絡

2010-08-13 10:50:12

FlashFlexWebService

2009-08-21 14:33:15

C#異步傳輸字符串
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 天堂亚洲 | 日韩在线视频免费观看 | 欧美自拍第一页 | 国产成人精品久久 | 亚洲日韩中文字幕一区 | 中文在线www | 国产精品一级在线观看 | 超碰在线网站 | 精品美女视频在线观看免费软件 | 视频一区在线 | 久久午夜精品福利一区二区 | 高清视频一区二区三区 | 久久久久国产一区二区三区四区 | 亚洲国产成人精品久久久国产成人一区 | 黄一区二区三区 | 毛片久久久 | www.久 | 超碰电影 | 国产精品亚洲精品久久 | 在线观看中文字幕 | 在线中文字幕亚洲 | 午夜小电影 | 999免费网站 | 日韩精品一区二区三区在线播放 | 成人影 | 一区二区三区在线播放 | 午夜视频大全 | 国产电影一区二区三区爱妃记 | 在线观看日本网站 | 久草视频在线播放 | 国产免费一区二区 | 国产久| 久久久久国产一区二区三区 | 成人网av | 成人av观看 | 日本久久精 | 国产精品久久久久久亚洲调教 | 国产精品视频一区二区三 | 国产精品久久久久久久久图文区 | 中国美女一级黄色片 | 中文字幕av亚洲精品一部二部 |