web service调用方式

简介: 第一种静态调用web service的方法try { //创建webservice命名空间 javax.

第一种静态调用web service的方法

try {
    //创建webservice命名空间
    javax.xml.namespace.QName SERVICE_NAME =  new QName("http://tempuri.org/", "cc2erp");
  //Cc2Erp,Cc2ErpSoap是用cxf框架生成的实例
    Cc2Erp ccErp = new Cc2Erp(new URL(Constants.CON_SERVICE_URL),SERVICE_NAME);
    Cc2ErpSoap port = ccErp.getCc2ErpSoap();  
  //tok是用来发送请求的用户名和密码
    String tok = port.getToken("test","fesco");
  //用来获取需要调用的json串
    String Json = 需要获取的数据,这个方法中将数据转为json串传递;
  //将tok和json串传给web service
    createResult = port.createOutbandTask(tok, taskInstJson);
    log.fine("CreateOutbandTask : "+createResult);
                    
} catch (Exception e) {
    e.printStackTrace();
    createResult = false;
    log.fine(e);
}
第二种通过http方式动态调用web service的方法

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.ConnectionType;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

public class Utils {
//动态调用wsdl客户端工厂
    private static final JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//设置最大连接时间
    private static final long CONNECTION_TIME = 30000;
//设置关闭
    private static final ConnectionType CONNECTION_TYPE = ConnectionType.CLOSE;
    //创建客户端
    private static Client getClient(String url){
        return dcf.createClient(url);
    }
    //创建通过http连接web service
    public static Object[] invoke(String url,String methodName,Object... params) throws Exception{
        Client client = getClient(url);
//设置HTTP连接管道
        HTTPConduit http = (HTTPConduit) client.getConduit();
//设置HTTP连接政策
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();        
        httpClientPolicy.setConnectionTimeout(CONNECTION_TIME);
        httpClientPolicy.setConnection(CONNECTION_TYPE);
        http.setClient(httpClientPolicy);
//返回调用的方法名和参数
        return client.invoke(methodName, params);
    }
  
    public static void main( String[] args )
    {
        System.out.println(invoke("http://192.168.191.1:9001/fws-outbound-service/webservice/OutboundService?wsdl",
                     "getToken","fesco", "d31fae18a821e71fc004044d00ef4033")[0]);
    }
}
第三种通过cxf动态调用web service的方法

/**
         * 通过cxf框架动态调用web service
         */
public static void main(String args) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:9001/.../**?wsdl");  //创建客户端
        //client.
        Object[] res = client.invoke("getToken", "username", "userpwd");
        System.out.println("------------------");
        System.out.println( res[0] );
        System.out.println("------------------");
        
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setServiceClass(HelloWorld.class);
        factory.setAddress("http://localhost:9001/HelloWorld");
        HelloWorld clientHelloWorld = (HelloWorld) factory.create();
        String reply = clientHelloWorld.sayHi("HI");
        System.out.println("------------------");
        System.out.println(reply);
        System.out.println("------------------");
}

目录
相关文章
|
9月前
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
118 0
|
1月前
|
存储 缓存 算法
关于 Service Worker 和 Web 应用对应关系的讨论
关于 Service Worker 和 Web 应用对应关系的讨论
13 0
|
2月前
|
Java API Apache
Apache CXF生成WebService的客户端
Apache CXF生成WebService的客户端
|
6月前
|
JSON 安全 API
使用 ABAP sproxy 事务码生成的 Proxy 消费 Web Service
使用 ABAP sproxy 事务码生成的 Proxy 消费 Web Service
55 0
|
2月前
|
XML 网络架构 数据格式
Ruby 教程 之 Ruby Web Service 应用 - SOAP4R 2
Ruby Web Service 应用 - SOAP4R
24 5
|
2月前
|
XML Linux 网络架构
Ruby 教程 之 Ruby Web Service 应用 - SOAP4R 1
Ruby Web Service 应用 - SOAP4R
23 3
|
8月前
|
XML Java API
Java Web Service Get请求使用指南
Java Web Service Get请求使用指南 在当今互联网时代,Web Service已经成为了现代软件开发中不可或缺的一部分。而Java作为一种广泛使用的编程语言,自然也提供了丰富的工具和库来支持Web Service的开发。本文将为大家介绍如何使用Java编程语言进行Web Service的Get请求。
86 0
|
4月前
|
Java 数据库连接 Apache
SpringBoot整合CXF实现WebService
SpringBoot整合CXF实现WebService
125 0
|
7月前
ABAP Web Service 调用的一个例子
ABAP Web Service 调用的一个例子
29 0
|
11月前
boot+cxf,实现webservice
boot+cxf,实现webservice