加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice客户端专题文档

发布时间:2020-12-16 23:09:11 所属栏目:安全 来源:网络整理
导读:1、 技术简介 Web Service主要是为了使原来各孤立的站点之间的信息能够相互通信、共享而提出的一种接口。 Web Service所使用的是Internet上统一、开放的标准,如HTTP、XML、SOAP(简单对象访问协议)、WSDL等,所以Web Service可以在任何支持这些标准的环境

1、 技术简介
Web Service主要是为了使原来各孤立的站点之间的信息能够相互通信、共享而提出的一种接口。 Web Service所使用的是Internet上统一、开放的标准,如HTTP、XML、SOAP(简单对象访问协议)、WSDL等,所以Web Service可以在任何支持这些标准的环境(Windows,Linux)中使用。注:SOAP协议(Simple Object Access Protocal,简单对象访问协议),它是一个用于分散和分布式环境下网络信息交换的基于XML的通讯协议。在此协议下,软件组件或应用程序能够通过标准的HTTP协议进行通讯。它的设计目标就是简单性和扩展性,这有助于大量异构程序和平台之间的互操作性,从而使存在的应用程序能够被广泛的用户访问。


2、技术框架
3、分支及版本
4、客户端调用
4.1客户端axis1
4.1.1引入jar
axis.jar

commons-logging.jar

commons-discovery.jar

jaxrpc.jar;

?


4.1.2测试代码
package com;


import java.net.MalformedURLException;

import java.rmi.RemoteException;


import javax.xml.namespace.QName;

import javax.xml.rpc.ServiceException;


import org.apache.axis.client.Call;

import org.apache.axis.client.Service;


public class TestWebServiceClient {

public static void main(String[] args) throws Exception {

System.out.println(callService("aaa"));

}

public static String callService(String xmlMessage) throws Exception {

//创建服务请求对象

Service service = new Service();

Call call=null;

String res = "";

try {

call = (Call) service.createCall();

//如果要调用的方法有命名空间,需要new QName(命名空间,方法名)

call.setOperationName(new QName("http://control.order.ab.com","receiveData"));

call.setTargetEndpointAddress(new java.net.URL("http://localhost:8080/services/testWebservice?wsdl"));//

//call.setOperationName("receiveData");// 这是要调用的方法

res = (String) call.invoke(new Object[] { xmlMessage });

System.out.println(res);

} catch (ServiceException e) {

e.printStackTrace();

throw e;

} catch (MalformedURLException e) {

e.printStackTrace();

throw e;

} catch (RemoteException e) {

e.printStackTrace();

throw e;

}

return res;


}


}

4.2客户端axis2
4.2.1引入jar
axis2-adb-1.5.jar

axis2-kernel-1.5.jar

axiom-api-1.2.8.jar

commons-logging-1.0.4.jar

wsdl4j.jar

XmlSchema-1.4.3.jar

axiom-impl-1.2.8.jar

neethi-2.0.4.jar

axis2-transport-local-1.5.jar

axis2-transport-http-1.5.jar

commons-httpclient-3.1.jar

mail-1.4.jar

httpcore-4.1.2.jar

commons-codec-1.3.jar

?

4.2.2测试代码
package com;


import javax.xml.namespace.QName;


import org.apache.axis2.addressing.EndpointReference;


import org.apache.axis2.client.Options;


import org.apache.axis2.rpc.client.RPCServiceClient;


public class TestWebServiceClient2 {

?

public static void main(String[] args) {

// TODO Auto-generated method stub

String reqXml="";


String respXml=sendService(reqXml,"http://10.10.139.32:8001/yeePayProxy/services/orderController?wsdl","http://control.order.ab.com","receiveData");

System.out.println(respXml);

}

public static String sendService(String xmlStr,String url,String namespace,String method){


String xml=null;


try {

?


RPCServiceClient serviceClient = new RPCServiceClient();


Options options = serviceClient.getOptions();


EndpointReference targetEPR = new EndpointReference(url);


options.setTo(targetEPR);


// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值


QName opAddEntry = new QName(namespace,method);


// 参数,如果有多个,继续往后面增加即可,不用指定参数的名称


Object[] opAddEntryArgs = new Object[] {xmlStr};


// 返回参数类型,这个和axis1有点区别


// invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;


// 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];


// 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。


// 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}


// 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,


// 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同


Class[] classes = new Class[] { String.class };


xml=(String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0];


System.out.println(xml);

?

} catch (Exception e) {


e.printStackTrace();


long end = System.currentTimeMillis();


}


return xml;


}


}

?

4.3客户端XFire
4.3.1引入jar包
<classpathentry kind="lib" path="lib/xfire/xfire-all-1.2.6.jar"/>

<classpathentry kind="lib" path="lib/xfire/commons-logging-1.0.4.jar"/>

<classpathentry kind="lib" path="lib/xfire/wsdl4j-1.6.1.jar"/>

<classpathentry kind="lib" path="lib/xfire/XmlSchema-1.1.jar"/>

<classpathentry kind="lib" path="lib/xfire/jdom.jar"/>

<classpathentry kind="lib" path="lib/xfire/commons-httpclient-3.1.jar"/>

<classpathentry kind="lib" path="lib/xfire/commons-codec-1.3.jar"/>

4.3.2测试代码
public static String invokeCallService(String xmlStr,String method) throws Exception {

Client client = null;

String res="";

try {

client = new Client(new URL(url));

Object[] result1 = client.invoke(method,new Object[] {xmlStr});

System.out.println(result1[0]);

res=(String)result1[0];

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return res;

}

4.4客户端CXF
4.4.1引入jar包
<classpathentry kind="lib" path="lib/cxf/mini_and_spring/commons-logging-1.1.1.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/cxf-2.3.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/stax2-api-3.0.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/woodstox-core-asl-4.0.8.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/wsdl4j-1.6.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/neethi-2.0.4.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/XmlSchema-1.4.7.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/spring-core-2.5.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/spring-beans-2.5.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/spring-context-2.5.2.jar"/>


4.4.2无Spring
<classpathentry kind="lib" path="lib/cxf/mini_no_spring/cxf-2.3.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/msv-20050913.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/stax2-api-3.0.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/woodstox-core-asl-4.0.8.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/wsdl4j-1.6.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/XmlSchema-1.4.7.jar"/>

4.4.3测试代码
public static String invokeCallService(String xmlStr,String method) throws Exception {

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

Client client = dcf.createClient(url);

Object[] res = null;

try {

res = client.invoke(method,xmlStr);

} catch (Exception e) {

e.printStackTrace();

}

return (String) res[0];

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读