博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
调用webservice查询手机号码归属地信息
阅读量:7072 次
发布时间:2019-06-28

本文共 2211 字,大约阅读时间需要 7 分钟。

Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。在这里我们使用soap协议往webservice发送信息,然后得到webservice服务器返回过来的信息,以此来查询手机号码的归属地信息。

Web Services有很多服务提供商,在这里我们使用www.webxml.com.cn的,如图:

www.webxml.com.cn

点击进去,我们可以看到soap协议定义的内容。

国内号码归属地查询

接下来就是编写代码了。

package cn.mzba.service;
 
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
 
public class MobileService {
 
    public static String findAddress(String mobile) throws Exception {
        InputStream is = MobileService.class.getClassLoader()
                .getResourceAsStream("mobilesoap.xml");
        byte[] data = StreamTool.readStream(is);
        String xml = new String(data, "UTF-8");
        String soap = xml.replaceAll("\\$mobile", mobile);
        byte[] result = soap.getBytes("UTF-8");
        String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setConnectTimeout(5 * 1000);
 
        conn.setRequestProperty("Content-Type",
                "application/soap+xml; charset=utf-8");
        conn.setRequestProperty("Content-Length", String.valueOf(result.length));
        OutputStream os = conn.getOutputStream();
        os.write(result);
        os.flush();
        os.close();
 
        InputStream isSocp = conn.getInputStream();
        return parse(isSocp);
    }
 
    public static String parse(InputStream is) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "UTF-8");
        int event = parser.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
            case XmlPullParser.START_TAG:
                if ("getMobileCodeInfoResult".equals(parser.getName())) {
                    return parser.nextText();
                }
                break;
            }
            event = parser.next();
        }
        return null;
    }
}
package cn.mzba.service;
 
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
 
public class StreamTool {
    /**
     * 读取输入流数据
     * 
     * @param is
     * @return
     * @throws Exception
     */
 
    public static byte[] readStream(InputStream is) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buffer = new byte[2048];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        is.close();
        return os.toByteArray();
    }
}

转载地址:http://plzml.baihongyu.com/

你可能感兴趣的文章
App Store优化推广 如何提高海外搜索排名
查看>>
POJ3580 SuperMemo(Splay的区间操作)
查看>>
Android Service解析
查看>>
elasticsearch快速入门
查看>>
CentOS7-部署kubernetes
查看>>
hdu 4001 To Miss Our Children Time( sort + DP )
查看>>
日常note
查看>>
Leetcode 727. Minimum Window Subsequence
查看>>
java切换jdk版本
查看>>
hdu 1005 Number Sequence zoj 1105
查看>>
VLAN
查看>>
Oracle12c 性能优化攻略:攻略1-2:创建具有最优性能的表空间
查看>>
yum install 报错[Errno 14] curl#37 - Couldn't open file /mnt/repodata/repomd.xml
查看>>
box-sizeing
查看>>
bzoj 3669 [Noi2014]魔法森林
查看>>
Linux中find、grep命令详细用法
查看>>
CentOS为中文显示
查看>>
Oracle迁移到DB2常用转换
查看>>
num 80
查看>>
Python 参数传递
查看>>