您的位置:首页 > 编程语言 > Java开发

java手机号归属地验证

2015-11-04 17:29 323 查看
前言:

使用背景:在项目使用过程中,客户需要项目接入移动、联通、电信三大运营商短信发送功能,但是对于非本地(此处为上海)手机号,由于权限不能发送,故要求在系统页面对手机号归属地作标识。

使用过程:

在查找资料过程中大概发现两种辨别方式,

第1种:通过httpclient向一些提供手机号归属地查询的网站网页灌入手机号码,对返回的内容依据正则解析得到 结果,但这种方法在亲自测试时,偶尔成功,大部分情况下都会报正则解析出错。

第2种:在百度文库中找到的,通过http://www.webxml.com.cn网站提供的webSerivice服务返回结果,测试时十分成功,代码也十分简洁,写成工具类,代码如下。

package com.founder.fwpt.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.NodeList;
/**
* 手机号归属地查询工具类
* @author zhaozuofa
* 2015-11-3 下午7:07:25
*
*/
public class JudeMobileAttribution {

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;
}

}

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("#");
System.out.println("手机号码输入有误");
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 = JudeMobileAttribution.getMobileNoTrack(Mobile);
if(str != null && !"".equals(str)){
str = str.substring(str.indexOf(":")+1);
String strArry [] = new String[]{};
strArry = str.split(" ");
str = strArry[0];
}
return str;
}

/*  public static void main(String[] args) {
//  System.out.println(Moblie.getSoapRequest("13272303204"));
//System.out.println(Moblie.getSoapInputStream("13226678785"));
//System.out.println(JudeMobileAttribution.getMobileNoTrack("13052297710"));
JudeMobileAttribution.getMobileAttribution("13052297710");
}*/

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: