查手机归属地

网上找的代码,自己留着用。
package com.lecast.pg.common.sms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* 通过手机号码,获得该号码的归属地
*
* @author zhaoxy
*
*/
public class MobileFromUtil {
//正则表达式,抽取手机归属地
public static final String REGEX_GET_MOBILE=
"(?is)(<tr[^>]+>[\\s]*<td[^>]+>[\\s]*卡号归属地[\\s]*</td>[\\s]*<td[^>]+>([^<]+)</td>[\\s]*</tr>)"; //2:from
//正则表达式,审核要获取手机归属地的手机是否符合格式,可以只输入手机号码前7位
public static final String REGEX_IS_MOBILE=
"(?is)(^1[3|4|5|8][0-9]\\d{4,8}$)";

/**
* 获得手机号码归属地
*
* @param mobileNumber
* @return
* @throws Exception
*/
public static String getMobileFrom(String mobileNumber) throws Exception {
if(!veriyMobile(mobileNumber)){
throw new Exception("不是完整的11位手机号或者正确的手机号前七位");
}
HttpClient client=null;
PostMethod method=null;
NameValuePair mobileParameter=null;
NameValuePair actionParameter=null;
int httpStatusCode;

String htmlSource=null;
String result=null;


try {
client=new HttpClient();
client.getHostConfiguration().setHost("www.ip138.com", 8080, "http");
method=new PostMethod("/search.asp");
mobileParameter=new NameValuePair("mobile",mobileNumber);
actionParameter=new NameValuePair("action","mobile");
method.setRequestBody(new NameValuePair[] { actionParameter,mobileParameter });
//设置编码
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GB2312");

client.executeMethod(method);
httpStatusCode=method.getStatusLine().getStatusCode();
if(httpStatusCode!=200){
throw new Exception("网页内容获取异常!Http Status Code:"+httpStatusCode);
}

htmlSource=method.getResponseBodyAsString();
if(htmlSource!=null&&!htmlSource.equals("")){
result=parseMobileFrom(htmlSource);
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
method.releaseConnection();
}

return result;

}


/**
* 从www.ip138.com返回的结果网页内容中获取手机号码归属地,结果为:省份 城市
*
* @param htmlSource
* @return
*/
public static String parseMobileFrom(String htmlSource){
Pattern p=null;
Matcher m=null;
String result=null;

p=Pattern.compile(REGEX_GET_MOBILE);
m=p.matcher(htmlSource);

while(m.find()){
if(m.start(2)>0){
result=m.group(2);
result=result.replaceAll(" ", " ");
}
}


return result;

}

/**
* 验证手机号
* @param mobileNumber
* @return
*/
public static boolean veriyMobile(String mobileNumber){
Pattern p=null;
Matcher m=null;

p=Pattern.compile(REGEX_IS_MOBILE);
m=p.matcher(mobileNumber);

return m.matches();
}

/**
* 测试
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// System.out.println(getMobileFrom("159000000"));
// BufferedReader bufferedReader = new BufferedReader(new FileReader(
// "c:a.TXT"));
// String str = null;
// while ((str = bufferedReader.readLine()) != null) {
// if (str.length()==11) {
// GetMobileMarkFromWAPIP138(str);
// }else{
// System.out.println(str);
// }
// }
GetMobileMarkFromWAPIP138("132000000");

}
public static void GetMobileMarkFromWAPIP138(String mobile7){
System.out.print(mobile7+" ");
// 请求URL
String REQUEST_URL = "http://wap.ip138.com/sim.asp";
// 请求方法
String REQUEST_MOTHOD = "GET";
BufferedReader br = null;

try {
HttpURLConnection httpConn = (HttpURLConnection) new URL(REQUEST_URL).openConnection();
httpConn.setRequestMethod(REQUEST_MOTHOD);
httpConn.setDoOutput(true);

String requestParameter = "mobile=" + mobile7;
httpConn.getOutputStream().write(requestParameter.getBytes());
httpConn.getOutputStream().flush();
httpConn.getOutputStream().close();

br = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));

String lineStr = null;

while ((lineStr = br.readLine()) != null) {
//Log.writeLog(lineStr);
lineStr = lineStr.toLowerCase().trim();
if(lineStr.equalsIgnoreCase("刷新太频繁<br/>")){
// return null;
}
if (lineStr.indexOf("归属地") != -1) {

lineStr = lineStr.replace("<br/>", "");
lineStr = lineStr.replace("归属地:", "");

String[] areas = lineStr.split(" ", 2);
if (areas.length > 1) {
System.out.print(areas[0]+" "+areas[1]+" ");
// mobileMarkInfo.setProvince(areas[0]);
// if (mobileMarkInfo.getProvince().equalsIgnoreCase("内蒙")) {
// mobileMarkInfo.setProvince("内蒙古");
// }
// mobileMarkInfo.setCity(areas[1]);
} else {
// return null;
}
}
// 获取卡类型
if (lineStr.indexOf("卡类型") != -1) {
lineStr = lineStr.replace("卡类型:", "");
lineStr = lineStr.replace("<br/>", "");
System.out.println(lineStr);
//mobileMarkInfo.setMobileType(lineStr);
}
}

} catch (Exception e) {
// e.printStackTrace();
// mobileMarkInfo = null;
System.out.println("MobileUtil.GetMobileMarkFromWAPIP138 " + e.getMessage());
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
// e.printStackTrace();
System.out.println("MobileUtil.GetMobileMarkFromWAPIP138 " + e.getMessage());
}
}
// return mobileMarkInfo;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值