webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。

1.可以通过带有webservice插件的Eclipse直接生成调用webservice客户端代码

在Eclipse中生成webservice客户端代码,New---->Other---->Webservice---->Webservice Client,选择之前拷贝到eclipse中的wsdl路径点击finish,这样eclipse就帮我们自动生成了webservice的客户端,接下来只需在程序中调用即可,在程序中调用eclipse自动生成的webservice客户端;

    

生成的客户端代码:

测试案例:

2.使用axis1.4或2.X生成webservice的客户端代码

1、下载axis1.4,解压; 
2、在axis-1_4目录下新建wsdl2java-client.bat(.bat批处理文件,可任意命名)文件,增加内容如下:

set Axis_Lib=.\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java   -p com.lmb.client.ws C:\Users\Administrator\Desktop\axis-1_4\axis-1_4\lmbtest.xml
pause

注意:其中com.lmb.client.ws为生成的客户端代码的包路径,C:\Users\Administrator\Desktop\axis-1_4\axis-1_4\lmbtest.xml为wsdl文件。

3、双击wsdl2java-client.bat:

可以看到相关路径下生成的客户端代码如下:

4、调用方法如下:

public class WebServiceClientTest{public static void main(String[] args){String wsdl = "http://xxx.xxx.xx.xx:8082/csp/services/c_lttb/orderToHeLiWebservice";String requestStr = "";// 有些webservice需要登录,登陆后才能进行一些操作,这个需要设置如下两个参数: //1、 超时时间 stub.setTimeout(1000 * 60 * 20); //2、 次数设置true,登录后才能保持登录状态,否则第二次调用ws方法时仍然会提示未登录。 stub.setMaintainSession(true);org.apache.axis.client.Service service = new org.apache.axis.client.Service();OrderToHeLiWebserviceHttpBindingStub stub = new OrderToHeLiWebserviceHttpBindingStub(new java.net.URL(wsdl), service);String response = stub.urgeWorkOrderServiceSheet(requestStr); //调用ws提供的方法System.out.println("response >>> " + response);}
}

3.手写客户端代码方式

新建一个maven工程

pom.xml添加jar依赖:

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!--axis2 begin--><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-spring</artifactId><version>${axis2.version}</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-transport-http</artifactId><version>${axis2.version}</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-transport-local</artifactId><version>${axis2.version}</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-xmlbeans</artifactId><version>${axis2.version}</version></dependency><!--axis2 end--><dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.9.7</version></dependency><dependency><groupId>org.jdom</groupId><artifactId>jdom</artifactId><version>2.0.2</version></dependency><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.3</version></dependency><dependency><groupId>javax.mail</groupId><artifactId>javax.mail-api</artifactId><version>1.4.7</version></dependency><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><!-- https://mvnrepository.com/artifact/axis/axis --><dependency><groupId>axis</groupId><artifactId>axis</artifactId><version>1.4</version></dependency></dependencies>

客户端代码:

(1)这里是用axis调用的 我自己写的代码 由于我的服务端接口比较老 所以我采用这种方式成功上传了数据 但是传输的附件大小只能不超过20k,超过了就报系统找不到指定的路径,暂时不知道为什么,希望知道的人告知下

package com.TestWebService.WebService;import java.io.File;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;public class AxisToWebServiceImportData {public String invokeByAxis() {boolean result = false;Service service = new Service();String endPoint = "http://192.168.0.***:8080/ESWebService/services/DataInterfaceService";try {Call call = (Call) service.createCall();call.setTargetEndpointAddress(endPoint);call.setEncodingStyle("utf-8");String srcFilePath = "D:\\sip-data.zip";String userName = "admin";String password = "*****";String flk_id = "27";String wjm = "wj1327";String zipName = "sip-data.zip";String encode = "92BBE4B3******067E9E5F17";DataHandler dh = new DataHandler(new FileDataSource(srcFilePath));QName qnameattachment = new QName("DataInterfaceService", "DataHandler");call.registerTypeMapping(dh.getClass(),qnameattachment,JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class);call.addParameter("dh", qnameattachment, javax.xml.rpc.ParameterMode.IN);call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);call.addParameter("password", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);call.addParameter("flk_id", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);call.addParameter("wjm", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);call.addParameter("zipName", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);call.addParameter("encode", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);call.setReturnType(org.apache.axis.encoding.XMLType.XSD_BOOLEAN);call.setOperationName(new QName(endPoint,"importData"));result = (Boolean) call.invoke(new Object[]{dh,userName,password,flk_id,wjm,zipName,encode});System.out.println("返回值:" + result);} catch (Exception e) {System.out.print("WebService请求异常!! ");e.printStackTrace();}return "true";}public static void main(String[] args) {AxisToWebServiceImportData a = new AxisToWebServiceImportData();a.invokeByAxis();}
}

(2)第二种方式是用RPC方式调用

package com.TestWebService.WebService;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.DOMBuilder;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.DOMOutputter;
import org.jdom2.xpath.XPath;public class ImportData {public boolean TestWebService() {boolean result = false;try {RPCServiceClient serviceClient = new RPCServiceClient();String url = "http://192.168.0.207:8080/ESWebService/services/DataInterfaceService";// 指定调用WebService的URLEndpointReference targetEPR = new EndpointReference(url);Options options = serviceClient.getOptions();// 确定目标服务地址options.setTo(targetEPR);// 确定调用方法options.setAction("importData");options.setProperty(HTTPConstants.CHUNKED, "false");// 把chunk关掉后,会自动加上Content-Length//解决高并发链接超时问题options.setManageSession(true);options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);//设置响应超时,默认5soptions.setProperty(HTTPConstants.SO_TIMEOUT, 5000);//设置连接超时,默认5soptions.setProperty(HTTPConstants.CONNECTION_TIMEOUT, 5000);String srcFilePath = "D:\\sip-data.zip";String userName = "admin";String password = "*****";String flk_id = "27";String wjm = "wj1327";String zipName = "sip-data.zip";String encode = "92BBE4B*****93A067E9E5F17";DataHandler dh = new DataHandler(new FileDataSource(new File(srcFilePath)));System.out.println("dh:::" + dh);// 指定方法的参数值Object[] parameters = new Object[] {dh,userName,password,flk_id,wjm,zipName,encode};// 创建服务名称// 1.namespaceURI - 命名空间地址 (wsdl文档中的targetNamespace)// 2.localPart - 服务视图名 (wsdl文档中operation的方法名称,例如<wsdl:operation name="getMobileCodeInfo">)QName qname = new QName("http://192.168.0.***:8080/ESWebService/services/DataInterfaceService", "importData");// 指定方法返回值的数据类型的Class对象Class[] returnTypes = new Class[] {Boolean.class};Object[] response = serviceClient.invokeBlocking(qname, parameters,returnTypes);
//            OMElement element = serviceClient.invokeBlocking(qname, parameters);
//            System.out.println("element::" + element);/** 值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。* 我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果*/
//            result = element.getFirstElement().getText();
//            System.out.println(result);result =   (Boolean) response[0];} catch (AxisFault e) {// TODO 自动生成的 catch 块e.printStackTrace();}return result;}public static void main(String[] args) {ImportData t = new ImportData();System.out.println("result:::"+ t.TestWebService());}
}

(3) 应用document方式调用 用ducument方式应用现对繁琐而灵活,引入相应的axis2的jar包

package com.TestWebService.WebService;import java.io.File;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;public class ImportDataTwo {public boolean TestImportData() {boolean res = false;try {ServiceClient serviceClient = new ServiceClient();//创建服务地址WebService的URL,注意不是WSDL的URLString url = "http://192.168.0.207:8080/ESWebService/services/DataInterfaceService";EndpointReference targetEPR = new EndpointReference(url);Options options = serviceClient.getOptions();options.setTo(targetEPR);//确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)options.setAction("http://192.168.0.207:8080/ESWebService/services/DataInterfaceService/importData");OMFactory fac = OMAbstractFactory.getOMFactory();/** 指定命名空间,参数:* uri--即为wsdl文档的targetNamespace,命名空间* perfix--可不填*/OMNamespace omNs = fac.createOMNamespace("http://192.168.0.***:8080/ESWebService/services/DataInterfaceService", "");// 指定方法OMElement method = fac.createOMElement("importData", omNs);// 指定方法的参数OMElement userName = fac.createOMElement("userName", omNs);userName.setText("admin");OMElement password = fac.createOMElement("password", omNs);password.setText("*****");String srcFilePath = "D:\\sip-data.zip";DataHandler dh2 = new DataHandler(new FileDataSource(new File(srcFilePath)));OMText textData = fac.createOMText(dh2, true);OMElement dh = fac.createOMElement("dh", omNs);dh.addChild(textData);System.out.println("dh:::" + dh2);OMElement flk_id = fac.createOMElement("flk_id", omNs);flk_id.setText("27");OMElement wjm = fac.createOMElement("wjm", omNs);wjm.setText("wj1327");OMElement zipName = fac.createOMElement("zipName", omNs);zipName.setText("sip-data.zip");OMElement encode = fac.createOMElement("encode", omNs);encode.setText("92BBE4B3BC*****393A067E9E5F17");method.addChild(dh);method.addChild(userName);method.addChild(password);method.addChild(flk_id);method.addChild(wjm);method.addChild(zipName);method.addChild(encode);// method.build();System.out.println(method);//远程调用web服务OMElement result = serviceClient.sendReceive(method);System.out.println(result);} catch (AxisFault axisFault) {axisFault.printStackTrace();}return true;}public static void main(String[] args) {ImportDataTwo t = new ImportDataTwo();t.TestImportData();}
}

暂时只接触到这么多webservice调用方式,码下来当作学习笔记了

java调用webService接口的几种方法相关推荐

  1. cmd 调用webservice接口_c# 三种方法调用WebService接口

    1.引用*.wsdl文件 WebService服务端会提供wsdl文件,客户端通过该文件生成.cs文件以及生成.dll. 注意:若服务端只提供的了URL,在URL后面加上"?wsdl&quo ...

  2. java 调用webservice 接口,并解析返回参数

    java 调用webservice 接口,并解析返回参数 1. 设置传参,例如以下格式: // 确定传参格式以及赋值 String reqXml = "<createAppParam& ...

  3. java调用接口速度慢,Java 调用 webservice 接口,为什么这么慢

    Java 调用 webservice 接口,为什么这么慢,有没有好的优化方案,本人目前改成 http 方式发 soap 消息调用,速度依旧很慢,维持在平均 100ms 下不来. 之前用 cxf 的 J ...

  4. 【转】java调用http接口的几种方式总结

    java调用http接口的几种方式总结 本文参考: https://blog.csdn.net/fightingXia/article/details/71775516 https://www.cnb ...

  5. Apache CXF生成java代码用java调用Webservice接口

    这里写自定义目录标题 使用java调用webservice接口 使用java调用webservice接口 当我们使用java去访问webservice的接口时,有很多种方式,这里我们使用apache ...

  6. [亲测可用]springBoot调用对方webService接口的几种方法示例

    目录 前言 一.需要用到的maven 二.如何调用webservice接口 调用方法一: 调用方法二: myEclipse生成的例子: idea生成的例子: 前言 平常我们开发调用接口一般会用到几种数 ...

  7. java调用webservice接口(.asmx)

    前几天对接阿里云短信接口时,需要调用其他系统的数据,该系统开发人员给我提供了webservice接口.对于从来没见过webservice接口的我,真的是一脸懵.现在把记录一下,方便自己以后用,说不定也 ...

  8. java 调用webservice接口

    RPC调用webservice接口 maven支持包: <dependency><groupId>javax.xml.rpc</groupId><artifa ...

  9. vue 调用webservice_动态调用WebService接口的几种方式

    一.什么是WebService? 这里就不再赘述了,想要了解的====>传送门 二.为什么要动态调用WebService接口? 一般在C#开发中调用webService服务中的接口都是通过引用过 ...

最新文章

  1. HDU 1251(trie树)
  2. 交换机知识--集群管理
  3. iOS中使用OpenGL 实现增高功能
  4. node更新到最新版本_云顶之弈10.24版本临时更新永恩、劫大砍,最新上分阵容推荐...
  5. Docker + Zookeeper + SolrCloud(8.1.1)跨主机搭建集群有问题
  6. Docker容器的运行时性能成本是多少?
  7. 【个人笔记】OpenCV4 C++ 快速入门 23课
  8. 微信小程序chooseMedia应用
  9. C#多线程操作界面控件的解决方案(转)
  10. Visual Studio 20052008 各个版本下载
  11. RV1109人脸识别门禁闸机主板方案
  12. opencv保存设像头图片时调整白平衡功能
  13. 提升业务投入和研发人效,2个实用建议,很多大公司都在用
  14. 数字信号处理之数字混频
  15. 上汽赛可携手几维安全 赋能移动出行安全新业态
  16. Hibernate的Disjunction和Conjunction
  17. SpringBoot后台java下载文件及注意的地方
  18. 天锋w2019_足以乱真的复刻:天锋W2019,堪比原作的外观设计和配置
  19. K_A02_001 基于单片机驱动4位数码管模块(74HC595) 0-3滚动+ 时钟显示
  20. 文献阅读:Scaling Instruction-Finetuned Language Models

热门文章

  1. C#作为客户端http通讯
  2. Python源代码保密、加密、混淆
  3. Java课设对对碰_JAVA课程设计题目
  4. 组播屏幕共享、Android屏幕共享开发小结
  5. Libratone小鸟音响正式发布Zipp 2智能家用音响系列
  6. python枚举算法流程图_算法-枚举
  7. 金刚经 原文及受持要领
  8. 应用密码学第一章绪论笔记
  9. 计算机病毒的格式是什么样的,文件病毒的格式是什么
  10. 等保测评方案怎么做?按照这个流程来,轻松又省心!