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("------------------");
}

目录
相关文章
|
6天前
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
|
7天前
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
6天前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
7天前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
|
7天前
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
|
7天前
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
|
7天前
|
存储 安全 网络安全
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
|
7天前
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
|
7天前
【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
【Azure 云服务】Azure Cloud Service 为 Web Role(IIS Host)增加自定义字段 (把HTTP Request Header中的User-Agent字段增加到IIS输出日志中)
|
7天前
|
Web App开发 安全 JavaScript
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
下一篇
云函数