调用第三方webservice服务,获取电话号码归属地简单例子

调用第三方webservice服务,获取电话号码归属地


方法1、http-get方式访问webservice

说明:运行main中的方法可以测试号码归属地的结果

package com.ws.one;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 
 * @ClassName MobileCodeService
 * @Description 调用第三方webservice服务,获取电话号码归属地
 * @author Try_go
 * @Date 2017年4月5日 下午9:26:38
 * @version 1.0.0
 */
public class MobileCodeService {
    // 1.http-get方式访问webservice
    public void get(String mobileCode, String userID) throws Exception {
        // 由http://www.webxml.com.cn/zh_cn/index.aspx网址中的调用说明知道url
        URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="
                + mobileCode + "&userID=" + userID);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(5000);
        connection.setRequestMethod("GET");
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { // 结果码=200
            InputStream is = connection.getInputStream();
            ByteArrayOutputStream boas = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = is.read(buffer)) != -1) {
                boas.write(buffer, 0, len);
            }
            System.out.println("GET请求获取数据:" + boas.toString());
            boas.close();
            is.close();
        }
    }

    /**
     * @Description 输入测试数据运行,看输出结果
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        MobileCodeService ws = new MobileCodeService();
        ws.get("13071815533", "");
    }
}

方法 2、使用wsimport生成本地代理来访问webservice

说明:这里先通过wsimport生成本地代理的代码,然后把代码加入到项目中使用即可。

package com.ws.two;

import java.util.List;

/**
 * 
 * @ClassName Test
 * @Description 使用wsimport生成本地代理来访问webservice
 *              方法查看wsdl的说明:网址:http://ws.webxml.com.cn/WebServices/MobileCodeWS.
 *              asmx?WSDL 生成方法: 1、在命令提示符中输入:wsimport -s ./ -p com.ws.two
 *              http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
 *              然后在桌面或c盘中的用户中找到文件 说明:-s ./ 表示要转成java文件,不写生成class文件 -p 表示输出包名 -d
 *              表示输出的路径
 * 
 * @author Try_go
 * @Date 2017年4月6日 下午9:44:22
 * @version 1.0.0
 */
public class Test {
    public static void main(String[] args) {
        // 生成服务对象
        MobileCodeWS ws = new MobileCodeWS();
        // 取得webservice服务的访问方式
        MobileCodeWSSoap mobileCodeWSSoap = ws.getMobileCodeWSSoap();
        // 得到结果:1简单的数据
        String result = mobileCodeWSSoap.getMobileCodeInfo("13071815544", "");
        System.out.println("返回结果result:" + result);
        // 得到结果:2复合的数据
        ArrayOfString databaseInfo = mobileCodeWSSoap.getDatabaseInfo();
        List<String> listResult = databaseInfo.getString();
        for (String temp : listResult) {
            System.out.println("结果,复合数据:" + temp);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值