网上有一个查询手机号码归属地的WebService接口,以下就用这个接口作为例子
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
环境准备
首先下载axis2-bin
http://axis.apache.org/axis2/java/core/download.html
下载axis2-1.x.x-bin.zip
解压后配置环境变量
AXIS2_HOME = F:\tools\axis2-1.7.9
PATH 增加 %AXIS2_HOME%\bin
根据wsdl生成java类
用浏览器打开http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
右键另存为MobileCodeWS.wsdl
打开cmd命令行
wsdl2java -p com.joyintech.interimpl.wsclient.webxml -uri MobileCodeWS.wsdl
红色部分根据实际情况替换,例如对方是ABS的接口就改为abs
将会在当前目录生成src文件夹,复制到interimpl_xxx项目中
调用WebService接口
我们现在要调用的接口是:
GetMobileCodeInfoResponse getMobileCodeInfo(GetMobileCodeInfo)
生成的代码中会有一个Stub结尾的类,打开这个类可以看到接口地址
public class MobileCodeTest {public static void main(String[] args) throws Exception {String webxmlHost = "ws.webxml.com.cn"; // 从配置文件中获取接口地址String url = "http://" + webxmlHost + "/WebServices/MobileCodeWS.asmx";MobileCodeWSStub stub = new MobileCodeWSStub(url);GetMobileCodeInfo params = new GetMobileCodeInfo();params.setMobileCode("13966660000");GetMobileCodeInfoResponse resp = stub.getMobileCodeInfo(params);System.out.println(resp.getGetMobileCodeInfoResult());}}
运行即可查询到13966660000的归属地信息:安徽 合肥 安徽移动动感地带卡
依赖包
<properties><axis2.version>1.7.9</axis2.version></properties><dependencies><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-transport-http</artifactId><version>${axis2.version}</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-transport-local</artifactId><version>${axis2.version}</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-xmlbeans</artifactId><version>${axis2.version}</version></dependency></dependencies>
