idea 调用 WebService

此处使用查询手机归属地的免费WebService
地址 : http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
第一步:
在idea 项目中任意包名右键(会在此包生成webservice的java类)
在这里插入图片描述
出现下面的对话框 填上wsdl 地址
在这里插入图片描述
生成下面这样的:

然后删除class 文件

第二步:
创建一个类,写上main方法

public static void main(String[] args) {
        //创建一个MobileCodeWS工厂
        MobileCodeWS factory = new MobileCodeWS();
        //根据工厂创建一个MobileCodeWSSoap对象
        MobileCodeWSSoap mobileCodeWSSoap = factory.getMobileCodeWSSoap();
        //调用WebService提供的getMobileCodeInfo方法查询手机号码的归属地
        String searchResult = mobileCodeWSSoap.getMobileCodeInfo("18512155752", null);
        System.out.println(searchResult);
        
        //***********************第二种调用方式
        //获取服务实现类
        MobileCodeWSSoap port = factory.getPort(MobileCodeWSSoap.class);
        //调用查询方法
        String reuslt = port.getMobileCodeInfo("13888881234", null);
        System.out.println(reuslt);
    }

测试完成


版权声明:本文为u010925982原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。