c# 手机号码归属地查询

rocket2010 rocket2010 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
此方法不是使用WebService。
而是直接使用提供号码归属地查询的网站,然后输入号码返回结果页面的办法,再用正则表达式提取需要的信息

[当然也可以使用WebService http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo 提供的 服务来查询,可是这个又限制使用的次数]

引用:
using System.Text.RegularExpressions;
using System.Net;

private void button1_Click(object sender, EventArgs e)
{
string phoneNum = tbPhoneNum.Text;
try
{
//web客户端
WebClient webclient = new WebClient();
//下载数据
string str_ = webclient.DownloadString("http://www.ip138.com:8080/search.asp?mobile=" + phoneNum + "&action=mobile");
//正则
string str_regex = "><!-- <td></td> -->.*</TD>";
//忽略大小写
Regex regex = new Regex(str_regex, RegexOptions.IgnoreCase);
//开始匹配
MatchCollection matchCollection = regex.Matches(str_);

txtAddress.Text = "卡归属地:" + matchCollection[0].Value.Replace("><!-- <td></td> -->","").Replace("</TD>","");

txtType.Text = "卡类型:" + matchCollection[1].Value.Replace("><!-- <td></td> -->", "").Replace("</TD>", "");
}
catch (Exception ex) { }
}
...全文
评论 4
收藏 已收藏
分享
返回顶部
rocket2010
C# 111,129 成员 642,540 内容