| 導購 | 订阅 | 在线投稿
分享
 
 
 

使用Java客戶端類調用c# WebService和xml rpc server

來源:互聯網網民  2008-05-31 12:10:47  評論

本文介紹一個非常實用的Java客戶端工具類來調用C# WebServices和apache XML rpc server,這個類的源碼是從網上下載的,我在博客網做項目的時候一直使用這個類來調試C# WebServices和MetaWeblog API。順便在這裏也給大家介紹一下C#如何處理此類發送的xml數據。

使用這個類不用安裝任何第三方工具,因爲采用http的方式發送xml文件,所以你只需要安裝好JDK就可以了。執行此類還可以獲得WebServices或xml rpc server返回的xml字符流,你可以根據返回的xml數據來進行其他程序處理。通過這種方式實現了Java平台和.NET平台的數據交換和WebService調用。

下面是此類的源代碼SOAPClient4XG.java:

/**

* SOAPClient4XG. Read the SOAP envelope file passed as the second

* parameter, pass it to the SOAP endpoint passed as the first parameter, and

* print out the SOAP envelope passed as a response. with help from Michael

* Brennan 03/09/01

*

*

* @author Bob DUCharme

* @version 1.1

* @param SOAPUrl URL of SOAP Endpoint to send request.

* @param xmlFile2Send A file with an XML document of the request.

*

* 5/23/01 revision: SOAPAction added

*/

import java.io.*;

import java.net.*;

public class SOAPClient4XG {

public static void main(String[] args) throws Exception {

if (args.length < 2) { //小于

System.err.println("Usage: java SOAPClient4XG " +

"http://soapURL soapEnvelopefile.xml" +

" [SOAPAction]");

System.err.println("SOAPAction is optional.");

System.exit(1);

}

String SOAPUrl = args[0];

String xmlFile2Send = args[1];

String SOAPAction = "";

if (args.length > 2) //大于

SOAPAction = args[2];

// Create the connection where we're going to send the file.

URL url = new URL(SOAPUrl);

URLConnection connection = url.openConnection();

HttpURLConnection httpConn = (HttpURLConnection) connection;

// Open the input file. After we copy it to a byte array, we can see

// how big it is so that we can set the HTTP Cotent-Length

// property. (See complete e-mail below for more on this.)

FileInputStream fin = new FileInputStream(xmlFile2Send);

ByteArrayOutputStream bout = new ByteArrayOutputStream();

// Copy the SOAP file to the open connection.

copy(fin,bout);

fin.close();

byte[] b = bout.toByteArray();

// Set the appropriate HTTP parameters.

httpConn.setRequestProperty( "Content-Length",

String.valueOf( b.length ) );

httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");

httpConn.setRequestProperty("SOAPAction",SOAPAction);

httpConn.setRequestMethod( "POST" );

httpConn.setDoOutput(true);

httpConn.setDoInput(true);

// Everything's set up; send the XML that was read in to b.

OutputStream out = httpConn.getOutputStream();

out.write( b );

out.close();

// Read the response and write it to standard out.

InputStreamReader isr =

new InputStreamReader(httpConn.getInputStream());

BufferedReader in = new BufferedReader(isr);

String inputLine;

while ((inputLine = in.readLine()) != null)

System.out.println(inputLine);

in.close();

}

// copy method from From E.R. Harold's book "Java I/O"

public static void copy(InputStream in, OutputStream out)

throws IOException {

// do not allow other threads to read from the

// input or write to the output while copying is

// taking place

synchronized (in) {

synchronized (out) {

byte[] buffer = new byte[256];

while (true) {

int bytesRead = in.read(buffer);

if (bytesRead == -1) break;

out.write(buffer, 0, bytesRead);

}

}

}

}

}

編譯: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裏的內容是調用的WebService方法的xml文件, http://tempuri.org是WebService方法的命名空間,一定要有,否則調用失敗,假如你在C# WebServices中使用了方法默認的命名空間的話,就使用http://tempuri.org,否則要與C#中定義的一致,UserLoginReq是C# WebServices的方法名。

 
特别声明:以上内容(如有图片或视频亦包括在内)为网络用户发布,本站仅提供信息存储服务。
 
  本文介紹一個非常實用的Java客戶端工具類來調用C# WebServices和apache XML rpc server,這個類的源碼是從網上下載的,我在博客網做項目的時候一直使用這個類來調試C# WebServices和MetaWeblog API。順便在這裏也給大家介紹一下C#如何處理此類發送的xml數據。 使用這個類不用安裝任何第三方工具,因爲采用http的方式發送xml文件,所以你只需要安裝好JDK就可以了。執行此類還可以獲得WebServices或xml rpc server返回的xml字符流,你可以根據返回的xml數據來進行其他程序處理。通過這種方式實現了Java平台和.NET平台的數據交換和WebService調用。 下面是此類的源代碼SOAPClient4XG.java: /** * SOAPClient4XG. Read the SOAP envelope file passed as the second * parameter, pass it to the SOAP endpoint passed as the first parameter, and * print out the SOAP envelope passed as a response. with help from Michael * Brennan 03/09/01 * * * @author Bob DUCharme * @version 1.1 * @param SOAPUrl URL of SOAP Endpoint to send request. * @param xmlFile2Send A file with an XML document of the request. * * 5/23/01 revision: SOAPAction added */ import java.io.*; import java.net.*; public class SOAPClient4XG { public static void main(String[] args) throws Exception { if (args.length < 2) { //小于 System.err.println("Usage: java SOAPClient4XG " + "http://soapURL soapEnvelopefile.xml" + " [SOAPAction]"); System.err.println("SOAPAction is optional."); System.exit(1); } String SOAPUrl = args[0]; String xmlFile2Send = args[1]; String SOAPAction = ""; if (args.length > 2) //大于 SOAPAction = args[2]; // Create the connection where we're going to send the file. URL url = new URL(SOAPUrl); URLConnection connection = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) connection; // Open the input file. After we copy it to a byte array, we can see // how big it is so that we can set the HTTP Cotent-Length // property. (See complete e-mail below for more on this.) FileInputStream fin = new FileInputStream(xmlFile2Send); ByteArrayOutputStream bout = new ByteArrayOutputStream(); // Copy the SOAP file to the open connection. copy(fin,bout); fin.close(); byte[] b = bout.toByteArray(); // Set the appropriate HTTP parameters. httpConn.setRequestProperty( "Content-Length", String.valueOf( b.length ) ); httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8"); httpConn.setRequestProperty("SOAPAction",SOAPAction); httpConn.setRequestMethod( "POST" ); httpConn.setDoOutput(true); httpConn.setDoInput(true); // Everything's set up; send the XML that was read in to b. OutputStream out = httpConn.getOutputStream(); out.write( b ); out.close(); // Read the response and write it to standard out. InputStreamReader isr = new InputStreamReader(httpConn.getInputStream()); BufferedReader in = new BufferedReader(isr); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } // copy method from From E.R. Harold's book "Java I/O" public static void copy(InputStream in, OutputStream out) throws IOException { // do not allow other threads to read from the // input or write to the output while copying is // taking place synchronized (in) { synchronized (out) { byte[] buffer = new byte[256]; while (true) { int bytesRead = in.read(buffer); if (bytesRead == -1) break; out.write(buffer, 0, bytesRead); } } } } } 編譯: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裏的內容是調用的WebService方法的xml文件, http://tempuri.org是WebService方法的命名空間,一定要有,否則調用失敗,假如你在C# WebServices中使用了方法默認的命名空間的話,就使用http://tempuri.org,否則要與C#中定義的一致,UserLoginReq是C# WebServices的方法名。
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有