码迷,mamicode.com
首页 > 移动开发 > 详细

手机号归属地查询工具类

时间:2017-11-27 15:17:31      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:==   ++   cat   工具类   turn   enc   new   doc   手机   

import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author:VIC
 * @Description: 手机归属地查询
 */
public class PhoneAddressUtil {
    private static String getSoapRequest(String mobileCode) {

        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "\n"
                + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " "
                + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + " "
                + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "\n"
                + "<soap:Body>" + "\n"
                + "<getMobileCodeInfo" + " " + "xmlns=\"http://WebXml.com.cn/\">" + "\n"
                + "<mobileCode>" + mobileCode + "</mobileCode>" + "\n"
                + "<userID></userID>" + "\n"
                + "</getMobileCodeInfo>" + "\n"
                + "</soap:Body>" + "\n"
                + "</soap:Envelope>"
        );
        return sb.toString();

    }

    private static InputStream getSoapInputStream(String mobileCode) {
        try {
            String soap = getSoapRequest(mobileCode);
            if (soap == null)
                return null;
            URL url = new URL("http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx");
            URLConnection conn = url.openConnection();
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
            conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getMobileCodeInfo");
            OutputStream os = conn.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
            osw.write(soap);
            osw.flush();
            osw.close();
            osw.close();
            InputStream is = conn.getInputStream();
            return is;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //返回示例 安徽 安庆 安徽联通GSM卡
    public static String getMobileNoTrack(String mobileCode) {
        try {
            org.w3c.dom.Document document = null;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            InputStream is = getSoapInputStream(mobileCode);
            DocumentBuilder db = dbf.newDocumentBuilder();
            document = db.parse(is);
            NodeList nl = document.getElementsByTagName("getMobileCodeInfoResult");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < nl.getLength(); i++) {
                org.w3c.dom.Node n = nl.item(i);
                if (n.getFirstChild().getNodeValue().equals("手机号码错误")) {
                    sb = new StringBuffer("#");
                    break;
                }
                sb.append(n.getFirstChild().getNodeValue() + "\n");
            }
            is.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取手机归属地 省份
    public static String getMobileAttribution(String Mobile){
        String str = "";
        str = PhoneAddressUtil.getMobileNoTrack(Mobile);
        if(str != null && !"".equals(str)){
            str = str.substring(str.indexOf(":")+1);
            String strArry [] = new String[]{};
            strArry = str.split(" ");
            if (strArry.length >= 2) {
                str = strArry[0]+strArry[1];
            } else {
                str = "无归属地信息";
            }
        }
        return str;
    }
}

  

手机号归属地查询工具类

标签:==   ++   cat   工具类   turn   enc   new   doc   手机   

原文地址:http://www.cnblogs.com/vicF/p/7903961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!