王朝网络
分享
 
 
 

使用Java客户端类调用c# WebService和xml rpc server

王朝c#·作者佚名  2008-05-31
宽屏版  字体: |||超大  

本文介绍一个非常实用的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的方法名。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有