自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Java客戶端類調(diào)用C# WebService

開發(fā) 后端
本文介紹一個(gè)非常實(shí)用的Java客戶端工具類來(lái)調(diào)用C# WebServices和apache xml rpc server,順便在這里也給大家介紹一下C#如何處理此類發(fā)送的xml數(shù)據(jù)。

使用這個(gè)類不用安裝任何第三方工具,因?yàn)椴捎胔ttp的方式發(fā)送xml文件,所以你只需要安裝好JDK就可以了。執(zhí)行此類還可以獲得WebServices或xml rpc server返回的xml字符流,你可以根據(jù)返回的xml數(shù)據(jù)來(lái)進(jìn)行其他程序處理。通過(guò)這種方式實(shí)現(xiàn)了Java平臺(tái)和.NET平臺(tái)的數(shù)據(jù)交換和Java客戶端類調(diào)用C# WebService。

下面是滿足Java客戶端類調(diào)用的源代碼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

運(yùn)行的命令格式: java -classpath . SOAPClient4XG

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

http://tempuri.org/UserLoginReq

不過(guò)先不要運(yùn)行上面的命令,先介紹一下命令行的意思,http://localhost/BokeServices/Service1.asmx是C# WebService的地址;

c:loginReq..xml里的內(nèi)容是Java客戶端類調(diào)用WebService方法的xml文件, http://tempuri.org是WebService方法的命名空間,一定要有,否則調(diào)用失敗,如果你在C# WebServices中使用了方法默認(rèn)的命名空間的話,就使用http://tempuri.org,否則要與C#中定義的一致,UserLoginReq是C# WebServices的方法名。注意xml文件中的方法名和參數(shù)名是與C# WebServices的方法名、參數(shù)名是一一對(duì)應(yīng)的(參數(shù)順序是可以顛倒的)。

我先介紹一個(gè)簡(jiǎn)單的例子(c:loginReq.xml),這個(gè)xml文件調(diào)用了遠(yuǎn)程C# WebService的UserLoginReq方法,并帶UserAcc(用戶名)和UserPwd(口令)兩個(gè)參數(shù),調(diào)用成功后C#會(huì)自動(dòng)返回一個(gè)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〉 

現(xiàn)在看一下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. }  

注意結(jié)構(gòu)UserLoginResp,C# WebServices返回SOAP信息時(shí)會(huì)自動(dòng)將UserLoginResp結(jié)構(gòu)轉(zhuǎn)換成xml的格式。

用此類做xml rpc server 的客戶端也很簡(jiǎn)單,下文是一個(gè)客戶端rpc.xml文件,調(diào)用了xml rpc server 端實(shí)現(xiàn)的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〉  

調(diào)用命令的格式:

java -classpath %CLASSPATH%;. SOAPClient4XG.

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

對(duì)比調(diào)用C# WebServices的命令行,可以看出調(diào)用xml rpc server的命令行少一個(gè)方法名參數(shù)。http://192.168.25.97:8080/BokeeXmlRpc/xml-rpc 是提供xml rpc 調(diào)用的server端servlet地址。

【編輯推薦】

  1. C# WebService發(fā)布與調(diào)用淺析
  2. 簡(jiǎn)明教程 C# Webservice實(shí)例
  3. C#中定義裝箱和拆箱詳解
  4. 淺談C#類型系統(tǒng)
  5. 三種不同的C#異常類型
責(zé)任編輯:冰荷 來(lái)源: it55
相關(guān)推薦

2009-08-06 17:12:13

C# WebServi

2020-03-19 08:00:00

客戶端KubernetesAPI

2009-08-21 15:59:22

服務(wù)端與客戶端通信

2009-08-21 16:14:52

服務(wù)端與客戶端通信

2009-08-21 15:36:41

服務(wù)端與客戶端

2009-08-21 15:54:40

服務(wù)端與客戶端

2009-08-06 17:57:14

C# webServiC# WebServi

2009-08-11 14:26:56

C#動(dòng)態(tài)調(diào)用WebSe

2015-07-09 10:44:48

C#WebService

2009-08-06 16:44:03

C#創(chuàng)建WebServ

2009-08-21 17:48:43

C#網(wǎng)絡(luò)編程

2009-08-21 17:53:25

C#網(wǎng)絡(luò)編程客戶端程序

2011-08-17 10:10:59

2011-08-25 10:37:15

leveldb的訪問(wèn)封C#客戶端源碼

2024-12-23 06:00:00

TCPC#網(wǎng)絡(luò)

2011-04-07 09:33:01

Activex

2012-10-11 17:02:02

IBMdw

2010-05-12 15:46:51

Subversion客

2009-08-21 14:33:15

C#異步傳輸字符串

2024-05-06 08:00:00

C#IP地址
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)