一、用CXF调用WebService的几种方式,参考:
http://cxf.apache.org/docs/how-do-i-develop-a-client.html
二、JaxWsProxyFactoryBean 与 JaxWsDynamicClientFactory
1、 JaxWsProxyFactoryBean
简介:
调用方式采用了和RMI类似的机制,即客户端直接服务器端提供的服务接口(interface),CXF通过运行时代理生成远程服务的代理对象,在客户端完成对webservice的访问;几个必填的字段:setAddress-这个就是我们发布webservice时候的地址,保持一致
缺点:
这种调用service的好处在于调用过程非常简单,就几行代码就完成一个webservice的调用,但是客户端也必须依赖服务器端的接口,这种调用方式限制是很大的,要求服务器端的webservice必须是java实现–这样也就失去了使用webservice的意义
public
class
Client {
public
static
void
main(String[] args) {- JaxWsProxyFactoryBean bean =
new
JaxWsProxyFactoryBean();- bean.setServiceClass(HelloWorldService.
class
);- bean.setAddress(“http://localhost:9090/helloWorldService”);
- HelloWorldService helloWorldService = (HelloWorldService)bean.create();
- String result = helloWorldService.sayHello(“Kevin”);
- System.out.println(result);
- }
2、JaxWsDynamicClientFactory
简介:
只要指定服务器端wsdl文件的位置,然后指定要调用的方法和方法的参数即可,不关心服务端的实现方式。
public
class
Client3 {
public
static
void
main(String[] args)
throws
Exception {- JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
- Client client = clientFactory.createClient(“http://localhost:9090/helloWorldService?wsdl”);
- Object[] result = client.invoke(“sayHello”, “KEVIN”);
- System.out.println(result[0]);
- }
- }
三、设置http参数
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy =
new
HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(
36000
);
httpClientPolicy.setAllowChunking(
false
);
httpClientPolicy.setReceiveTimeout(
32000
);
http.setClient(httpClientPolicy);
四、用cxf忽略证书调用htts的WebService方法
然后 Client client = clientFactory.createClient(wsdl)成功后,设置TLSClientParameters下面两个属性为true
setUseHttpsURLConnectionDefaultHostnameVerifier(boolean useHttpsURLConnectionDefaultHostnameVerifier)
setUseHttpsURLConnectionDefaultSslSocketFactory(boolean useHttpsURLConnectionDefaultSslSocketFactory)