java调用webservice接口 几种方法
webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。今天搜索到了非常好的 webservice provider列表
http://www.webservicex.net/WCF/default.aspx
这上面列出了70多个包括很多方面的free webservice provider,utilities->global weather就可以获取全球的天气预报。
下面我们来看Java如何通过WSDL文件来调用这些web service:
注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。
1,直接AXIS调用远程的web service
我觉得这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
- import java.util.Date;
- import java.text.DateFormat;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import javax.xml.namespace.QName;
- import java.lang.Integer;
- import javax.xml.rpc.ParameterMode;
- public class caClient {
- public static void main(String[] args) {
- try {
- String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
- // 直接引用远程的wsdl文件
- // 以下都是套路
- Service service = new Service();
- Call call = (Call) service.createCall();
- call.setTargetEndpointAddress(endpoint);
- call.setOperationName("addUser");// WSDL里面描述的接口名称
- call.addParameter("userName",
- org.apache.axis.encoding.XMLType.XSD_DATE,
- javax.xml.rpc.ParameterMode.IN);// 接口的参数
- call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
- String temp = "测试人员";
- String result = (String) call.invoke(new Object[] { temp });
- // 给方法传递参数,并且调用方法
- System.out.println("result is " + result);
- } catch (Exception e) {
- System.err.println(e.toString());
- }
- }
- }
2,直接SOAP调用远程的webservice
这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来
- import org.apache.soap.util.xml.*;
- import org.apache.soap.*;
- import org.apache.soap.rpc.*;
- import java.io.*;
- import java.net.*;
- import java.util.Vector;
- public class caService {
- public static String getService(String user) {
- URL url = null;
- try {
- url = new URL(
- "http://192.168.0.100:8080/ca3/services/caSynrochnized");
- } catch (MalformedURLException mue) {
- return mue.getMessage();
- }
- // This is the main SOAP object
- Call soapCall = new Call();
- // Use SOAP encoding
- soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
- // This is the remote object we're asking for the price
- soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
- // This is the name of the method on the above object
- soapCall.setMethodName("getUser");
- // We need to send the ISBN number as an input parameter to the method
- Vector soapParams = new Vector();
- // name, type, value, encoding style
- Parameter isbnParam = new Parameter("userName", String.class, user,
- null);
- soapParams.addElement(isbnParam);
- soapCall.setParams(soapParams);
- try {
- // Invoke the remote method on the object
- Response soapResponse = soapCall.invoke(url, "");
- // Check to see if there is an error, return "N/A"
- if (soapResponse.generatedFault()) {
- Fault fault = soapResponse.getFault();
- String f = fault.getFaultString();
- return f;
- } else {
- // read result
- Parameter soapResult = soapResponse.getReturnValue();
- // get a string from the result
- return soapResult.getValue().toString();
- }
- } catch (SOAPException se) {
- return se.getMessage();
- }
- }
- }
3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。
这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。
首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL
原本的网址是http://www.webservicex.net/globalweather.asmx?WSDL,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。
那么就会出现4个文件:
GlobalWeather.java
GlobalWeatherLocator.java
GlobalWeatherSoap.java
GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我们最为关心的接口文件,如果你对RMI等SOAP实现的具体细节不感兴趣,那么你只需要看接口文件即可,
在使用的时候,引入这个接口即可,就好像使用本地类一样。
java调用webservice接口 几种方法的更多相关文章
- Java调用webservice接口方法
java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ...
- php调用webservice的几种方法
原文:php调用webservice的几种方法 1.WSDL模式: $soap = new SoapClient("http://192.168.6.69:8899/Service1.asm ...
- Java调用webservice接口方法(SOAP message、xfire、axis)
webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使 ...
- java 调用webservice接口wsdl,推荐使用wsdl2java,放弃wsimport
网上说wsimport是jdk1.6后自带的客户端生成调用webservice接口的工具,其实我挺喜欢原生的东西,毕竟自家的东西用着应该最顺手啊,但往往让人惊艳的是那些集成工具. 本机jdk1.8.1 ...
- Java开发WebService的几种方法--转载
webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2 Axis是apache下一个开源的webservice开发组件 ...
- 通用的调用WebService的两种方法。(调用别人提供的wsdl)(转)
转载自:http://blog.sina.com.cn/s/blog_65933e020101incz.html1.调用WebService的Client端采用jax-ws调用WebService:流 ...
- C#调用webService的几种方法
转自: WebClient 用法小结 http://www.cnblogs.com/hfliyi/archive/2012/08/21/2649892.html http://www.cnblogs. ...
- java 调用webservice接口
webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使 ...
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
近来由于项目需要,需要用到手机短信验证码的功能,其中最主要的是用到了第三方提供的短信平台接口WebService客户端接口,下面我把我在项目中用到的记录一下,以便给大家提供个思路,由于本人的文采有限, ...
随机推荐
- java语言课堂动手动脑
1 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...
- wordcloud:让你的词语像云朵一样美
介绍 对文本中出现频率较高的关键词给予视觉化的显示 使用 python import jieba import codecs import wordcloud file = r"C:\U ...
- 3D Computer Grapihcs Using OpenGL - 17 添加相机(旋转)
在11节我们说过,MVP矩阵中目前只应用了两个矩阵,World to View 矩阵被省略了,这就导致我们的画面没有办法转换视角. 本节我们将添加这一环节,让相机可以旋转. 为了实现这一目的,我们添加 ...
- 聊聊spring-boot-starter-data-redis的配置变更
本文主要研究一下spring-boot-starter-data-redis的配置变更 配置变更 以前是spring-boot的1.4.x版本的(spring-data-redis为1.7.x版本), ...
- Comparable接口与Comparator接口的比较————总结
之前的两篇文章主要学习了Comparable接口和Comparator接口的学习.既然已经学习完了,现在就趁热打铁,进行总结吧! Comparable接口和Comparator接口的共同点: 1. 都 ...
- controller大全(推荐)
@Controller @RequestMapping("/router") @SessionAttributes(value = { "username" } ...
- 第三周课程总结&实验报告(一)
实验报告(一) 1.打印输出所有的"水仙花数",所谓"水仙花数"是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个"水仙花数" ...
- leetcode-easy-array-122 best time to buy and sell stocks II
mycode 69.45% class Solution(object): def maxProfit(self, prices): """ :type prices: ...
- 网络协议之TCP/IP协议
沙漏计时器型TCP/IP协议族,允许IP on everyting,即支持多种形式和物理层和数据链路层实现:同时支持多种多样的应用层协议,扩展了各式各样的服务. IP协议(网际协议) 与IP协议配套使 ...
- 搭建第一个netty程序
来自action In netty 自己修改一点点 主要依赖 <dependencies> <dependency> <groupId>io.netty</g ...