java调用C# webService发布的接口

java调用C# webService发布的接口

java调用C# webService方式有很多种我这里只介绍一种

首先需要引入axis的jar包 axis的maven坐标如下

<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>

也可以到这个地址搜索然后复制坐标输入即可   https://mvnrepository.com/

下面是调用列子代码:

 1   //接口地址
 2         String oaurl = Preferences.getInstance().getOAURL();
 3         String result = null;
 4         try {
 5             //命名空间我这里是例子代码随便写的命名空间地址(具体地址可以通过wsdl文件查看或者问接口提供方获取))
 6             String soapaction = "http://localhost.org/";
 7             Service service = new Service();
 8             //创建Call对象,Call对象用于调用服务
 9             Call call = (Call) service.createCall();
10             //设置webServiced的URL
11             call.setTargetEndpointAddress(oaurl);
12             //设置调用方法名
13             call.setOperationName(new QName(soapaction, "GetJson"));//设置要调用的方法名
14             //设置请求参数(str是接口的入参变量,具体查看你调用的接口)
15             call.addParameter(new QName(soapaction , "str"),
16                     org.apache.axis.encoding.XMLType.XSD_STRING,
17                     javax.xml.rpc.ParameterMode.IN);
18 
19            /**
20             * 也可以这样设置请求参数,具体根据接口入参方式选择
21             * call.addParameter(new QName(soapaction , "userName"),
22             *                     org.apache.axis.encoding.XMLType.XSD_STRING,
23             *                     javax.xml.rpc.ParameterMode.IN);
24             * call.addParameter(new QName(soapaction , "phone"),
25             *                     org.apache.axis.encoding.XMLType.XSD_STRING,
26             *                     javax.xml.rpc.ParameterMode.IN);
27             * */
28             //设置返回结果类型
29             call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
30             //call.setReturnType(new QName(soapaction, "GetJson"), String.class);
31             call.setUseSOAPAction(true);
32             call.setSOAPActionURI(soapaction + "GetJson");
33 
34             Map<String, Object> params = new HashMap<>();
35 
36             List<String> list = new ArrayList<>();
37 
38             String[] address = new String[]{"http://www.baidu.com/1.jpg"};
39             String[] fileName = new String[]{"附件1"};
40             String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
41             String[] fileDate = new String[]{dateStr};
42 
43             Collections.addAll(list , "2019-10-14 09:48:35.930", "slbh", "101000001", "hm", "20", "ch", "cnxh", "bw", "q6q", "gyh",
44                     "kj", "lxr", "dh", "符合申请");
45             params.put("flowname", "符合申请");
46             params.put("username", "pinlin");
47             params.put("data" , list);
48             params.put("attach_count", "1");
49             params.put("address" ,address);
50             params.put("filename" , fileName);
51             params.put("filedate", fileDate);
52 
53             //转换成JSON字符串
54             String strs = JSON.toJSONString(params);
55             System.out.println("str============" + strs);
56 
57             /**执行接口调用并得到返回接口
58              *
59              *  result = (String) call.invoke(new Object[]{"pinlin","123456899"} );
60              *  如果上面设置参数时设置多个的。传参顺序要设置的顺序要一致
61              * */
62             result = (String) call.invoke(new Object[]{strs});
63             System.out.println("result = " + result);
64         } catch (Exception e) {
65             e.printStackTrace();
66             log.error(e.toString());
67         }

如何查看通过接口地址得到wsdl,这里以天气预报公共的接口为列子,步骤如下

 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 

在接口地址后面加上?wsdl即可 这里介绍一些基本信息如下:

也可以直接地址查看接口有那些方法,步骤如下:

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 

 可以点击这些方法名进入里面会有说明调用方式,具体如下:

 

 

 调用C# webservice的发布的API也可以通过wsdl文件直接客户端代码调用,有apache-cxf,这个我使用过,但是生成的代码我调用不成功,不知道是不是我操作不对,具体问题这个我还在探索中。

本人水平有限也是我第一次补发博客文章,有哪里写不好或者不对的地方,还请大家多多指点。

由于第一次写博客,排版有些不好还请谅解。

猜你喜欢

转载自www.cnblogs.com/zengpinlin/p/11951711.html