Java客户端调用.NET的WebService实例

2025-05-29 0 40

项目需要去调用.net的websrevice,本身是java,研究了半天,终于有些头绪,记下来。

1,新建.net webservice。只在原方法上加上一个string类型的参数str

?

1

2

3

4

5
[webmethod]

public string helloworld(string str)

{

return "hello world";

}

2,新建java的webservice客户端,lib引入以下5个jar包(我是用idea生成的webservice客户端,会下载7个包,我试着删掉了log4j和saaj两个包也能正常运行)

Java客户端调用.NET的WebService实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
import org.apache.axis.client.call;

import org.apache.axis.client.service;

import org.apache.axis.encoding.xmltype;

import javax.xml.namespace.qname;

import javax.xml.rpc.parametermode;

public class helloworldclient {

public static void main(string[] argv) {

string endpoint ="http://localhost:64662/webservice1.asmx?wsdl";

try {

// 定义服务

service service = new service();

call call = (call) service.createcall();

call.settargetendpointaddress(endpoint);

call.setoperationname(new qname("http://tempuri.org/", "helloworld"));

call.setsoapactionuri("http://tempuri.org/helloworld");

call.addparameter(new qname("http://tempuri.org/", "str"),// 这里的str对应webservice参数名称

xmltype.soap_string, parametermode.in);

call.setreturntype(xmltype.soap_string);

string retval1 = (string) call.invoke(new object[] {"hello world!"});

system.out.println(retval1);

} catch (exception e) {

e.printstacktrace();

}

}

}

注:

1,网上看到有直接写成call.setoperationname("helloworld"),我试过不行。不知道是不是跨语言的原因。

2,网上也看到省略call.setsoapactionuri这 一句的,但我的报错了。

3,其实项目的webservice里面用的参数是实体,我试着java端通过xmltype.xsd_anytype类型传实体过去,结果说类型没注册之类的。网上看有方案比较繁琐,倒不如将实体序列化成json串传过去省事。

4,参数的命名空间参见服务页面

Java客户端调用.NET的WebService实例

以上这篇java客户端调用.net的webservice实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/nanchuan/archive/2017/09/16/7532007.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java客户端调用.NET的WebService实例 https://www.kuaiidc.com/114946.html

相关文章

发表评论
暂无评论