c# 手机号码归属地查询

rocket2010 2011-07-24 01:46:33
此方法不是使用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) { }
}
...全文
540 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
haoyan123 2012-02-18
  • 打赏
  • 举报
回复
引用System.Xml

public partial class GetMobileFromWhere : Form
{
public GetMobileFromWhere()
{
InitializeComponent();
}


private static string[] GetMobileInfo(string number)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("http://api.showji.com/Locating/default.aspx?m=" + number);
XmlNamespaceManager cx = new XmlNamespaceManager(xmlDocument.NameTable);
cx.AddNamespace("content", "http://api.showji.com/Locating/");
XmlNodeList nodes = xmlDocument.SelectNodes("//content:QueryResult|//content:Mobile|//content:Province|//content:City|//content:Corp|//content:Card|//content:AreaCode|//content:PostCode", cx);
if (nodes != null)
if (nodes.Count == 8)
{
if ("True".Equals(nodes[1].InnerText))
{

return new string[] { nodes[0].InnerText,nodes[2].InnerText,nodes[3].InnerText,nodes[4].InnerText,nodes[5].InnerText,nodes[6].InnerText + nodes[7].InnerText};
}
}
return null;
}
catch (Exception)
{
return null;
}
}

private void button1_Click(object sender, EventArgs e)
{
try
{
string[] num = GetMobileInfo(this.txtMobile.Text.Trim());
label2.Text = "所查号码:" + num[0];
label3.Text = "归属省份:" + num[1];
label4.Text = "归属城市:" + num[2];
label5.Text = "城市区号:" + num[3];
label6.Text = "城市邮编:" + num[4];
label7.Text = "卡 类 型: " + num[5];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

private void txtMobile_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(sender, e);
}
}



}
附图片

rocket2010 2011-07-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wy811007 的回复:]
直接用webBrowser 内嵌吧..
http://guishu.showji.com/BaiduApp/ 这个是百度的app 一个手机查询的
[/Quote]

我为什么要使用webBrowser 这个控件呢?多麻烦。
假如webBrowser 这个控件读取慢呢?怎么办??
wy811007 2011-07-24
  • 打赏
  • 举报
回复
直接用webBrowser 内嵌吧..
http://guishu.showji.com/BaiduApp/ 这个是百度的app 一个手机查询的
rocket2010 2011-07-24
  • 打赏
  • 举报
回复
先自己挺一下自己...

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧