Java调用以WSDL形式发布的web service.doc_第1页
Java调用以WSDL形式发布的web service.doc_第2页
Java调用以WSDL形式发布的web service.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java调用以WSDL形式发布的web servicewebservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。今天搜索到了非常好的 webservice provider列表/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 space.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 Objecttemp);/给方法传递参数,并且调用方法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 .*;import java.util.Vector;public class caServicepublic static String getService(String user) URL url = null;try url=new URL(00:8080/ca3/services/caSynrochnized); catch (MalformedURLException mue) return mue.getMessage();/ This is the main SOAP objectCall soapCall = new Call();/ Use SOAP encodingsoapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);/ This is the remote object were asking for the pricesoapCall.setTargetObjectURI(urn:xmethods-caSynrochnized);/ This is the name of the method on the above objectsoapCall.setMethodName(getUser);/ We need to send the ISBN number as an input parameter to the methodVector soapParams = new Vector();/ name, type, value, encoding styleParameter isbnParam = new Parameter(userName, String.class, user, null);soapParams.addElement(isbnParam);soapCall.setParams(soapParams);try / Invoke the remote method on the objectResponse soapResponse = soapCall.invoke(url,);/ Check to see if there is an error, return N/Aif (soapResponse.generatedFault() Fault fault = soapResponse.getFault();String f = fault.getFaultString();return f; else / read resultParameter soapResult = soapResponse.getReturnValue ();/ get a string from the resultreturn soapResult.getValue().toString(); catch (SOAPException se) return se.getMessage();3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。首先 java org.apache.axis.wsdl.WSDL2Java /globalweather.asmx.WSDL原本的网址是/globalweather.asmx?WSDL,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。那么就会出现4个文件:GlobalWeather.java GlobalWeath

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论