前台页面文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="获取手机归属地.aspx.cs" Inherits="实例_获取手机归属地" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
请输入手机号码:<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server"
Text="查 询" οnclick="Button1_Click" /><br />
<div id="div1" runat="server"></div>
</div>
</form>
</body>
</html>
后台页面文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class 实例_获取手机归属地 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string phoneNumber = TextBox1.Text;
if (phoneNumber != "")
{
div1.InnerHtml = "";
foreach (string item in GetInfoByxml(" http://vip.showji.com/locating/?m={0}", phoneNumber.Trim()))
{
div1.InnerHtml += "<br/>" + item;
}
}
else
{
//请输入手机号!
}
}
/// <summary>
/// 好123查询,符合下列规则也可使用
/// 返回xml
/// 需要顺序的节点:
/// QueryResult(查询结果状态True,False)
/// Province(所属省份)
/// City(所属地区)
/// Corp(服务商)
/// Card(卡类型 GSM)
/// AreaCode(区号)
/// PostCode(邮编)
/// </summary>
/// <param name="url"></param>
/// <param name="mobileNum"></param>
/// <returns></returns>
public static string[] GetInfoByxml(string url, string mobileNum)
{
try
{
XmlDocument xml = new XmlDocument();
<?xml version='1.0' encoding='utf-8' ?>
//<QueryResponse xmlns='http://api.showji.com/Locating/'>
//<Mobile>15890636739</Mobile>
//<QueryResult>True</QueryResult>
//<Province>河南</Province>
//<City>郑州</City>
//<AreaCode>0371</AreaCode>
//<PostCode>450000</PostCode>
//<Corp>中国移动</Corp>
//<Card>GSM</Card>
//</QueryResponse>
xml.Load(string.Format(url, mobileNum));
XmlNamespaceManager xmlNm = new XmlNamespaceManager(xml.NameTable);
xmlNm.AddNamespace("content", "http://api.showji.com/Locating/");
XmlNodeList nodes = xml.SelectNodes("//content:QueryResult|//content:Mobile|//content:Province|//content:City|//content:Corp|//content:Card|//content:AreaCode|//content:PostCode", xmlNm);
if (nodes.Count == 8)
{
if ("True".Equals(nodes[1].InnerText))
{
return new string[] { "手机号码:" + nodes[0].InnerText, "省份:" + nodes[2].InnerText , "地区:" + nodes[3].InnerText,"服务商:"+ nodes[6].InnerText ,"卡类型:"+ nodes[7].InnerText, "区号:"+nodes[4].InnerText, "邮编:"+nodes[5].InnerText };
}
}
return new string[] { "FALSE" };
}
catch
{
return new string[] { "FALSE" };
}
}
}
此例使用IE9自带的开发人员工具查询hao123的手机号码归属地的问题。
可参考:http://www.cnblogs.com/sufei/archive/2011/04/29/2033036.html