java调用WebService接口

方式一:利用AXIS调用远程的web service

import java.net.URL;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class Client {

    public static void main(String[] args) throws Exception {
        Call call=(Call)(new Service()).createCall();
        //URL中为webService暴露的接口地址
        call.setTargetEndpointAddress(new URL("http://IP:端口/XXX"));
        // namespace 为命名空间  方法名即为你要调用的方法
        call.setOperationName(new QName("namespace","方法名"));
        call.setTimeout(30000);
        // 参数 是你调用方法时传的参数
        String re=(String)call.invoke(new String[]{参数});
        System.out.println(re);
    }

}

Maven依赖:

<!-- axis 1.4 jar start -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- axis 1.4 jar end -->

方式二:利用CXF调用WebService接口

public static void main (String[] args){
        try{
            String webServiceUrl = "http://238.62.169.140:8090/g6_venues/webservice/gtv-external-venues-service?wsdl";
            JaxWsDynamicClientFactory factory1 = JaxWsDynamicClientFactory.newInstance();
            Client client = factory1.createClient(webServiceUrl);
            HTTPConduit conduit = (HTTPConduit)client.getConduit();
            HTTPClientPolicy clientPolicy = new HTTPClientPolicy();
            //连接超时
            clientPolicy.setConnectionTimeout(2000);
            //取消块编码
            clientPolicy.setAllowChunking(false);
            //响应超时
            clientPolicy.setReceiveTimeout(12000);
            conduit.setClient(clientPolicy);
            String startDateTime = "20201018090000";
            String endDateTime = "20201228160000";
            //("方法名","参数1","参数2",......)
            Object[] result = client.invoke("queryFreeRoomInfo",startDateTime ,endDateTime ); //queryFreeRoomInfo为方法名;
            System.out.println("返回结果:" + result[0].toString());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

Maven依赖:

JaxWsDynamicClientFactory、HTTPConduit、Client、HTTPClientPolicy需要引入对应的依赖,

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.10</version>
        </dependency>

 总结:两种方式都可调通webservice接口,但是重要的还是要把webservice生成的xml文件搞明白才行哦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值