获取手机号码归属地

通过webservices接口获取手机号码归属地:

获取手机号码归属地

引用webservices地址:

http://api.showji.com/locating/Mobile.asmx

主要代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Net;

using System.Text;

using System.Windows.Forms;

using System.Xml;



namespace MobileAttribution

{



    public partial class Form1 : Form

    {

        //Html网页代码

        private string htmlCode = string.Empty;

        //运营商

        private string corp = string.Empty;

        //卡类型

        private string card = string.Empty;



        ServiceMobile.MobileSoapClient mc = new ServiceMobile.MobileSoapClient();



        public Form1()

        {

            InitializeComponent();

                  

        }

       

         /// <summary>

         /// 查询号码归属地

          /// </summary>

        /// <param name="htmlCode">网页Html代码</param>

        private void QueryLocating(string htmlCode)

        {

            //创建Xml实例

            XmlDocument xmldoc = new XmlDocument();

            //加载Xml文档

            xmldoc.LoadXml(htmlCode);

            //获取Xml文档的根元素

            XmlElement root = xmldoc.DocumentElement;



            //获取Xml文档的根元素下的所有子节点

            XmlNodeList topNode = xmldoc.DocumentElement.ChildNodes;



            //子节点凑集

            XmlNodeList elemList;



            //遍历根元素下所有子节点

            foreach (XmlElement element in topNode)

            {

                //按照节点名称获取节点元素值

                elemList = root.GetElementsByTagName(element.Name);



                switch (element.Name)

                {

                    //断定号码格式是否正确

                    case "QueryResult":

                        if (elemList[0].InnerText.ToString() == "False")

                            MessageBox.Show("您输入的号码格式有误,请重新输入!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        continue;

                    //号码

                    case "Mobile":

                        txtMobile.Text = elemList[0].InnerText;

                        break;

                    //所属省份

                    case "Province":

                        txtProvince.Text = elemList[0].InnerText;

                        break;

                    //所属城市

                    case "City":

                        txtCity.Text = elemList[0].InnerText;

                        break;

                    //区号

                    case "AreaCode":

                        txtAreaCode.Text = elemList[0].InnerText;

                        break;

                    //邮编

                    case "PostCode":

                        txtPostCode.Text = elemList[0].InnerText;

                        break;

                    //运营商

                    case "Corp":

                        corp = elemList[0].InnerText;

                        break;

                    //卡类型

                    case "Card":

                        card = elemList[0].InnerText;

                        //拼接字符串(运营商+卡类型)

                        txtCard.Text = corp + card;

                        break;

                }

            }



        }



        /// <summary>

        /// 抓取网页html代码

         /// </summary>

        /// <param name="strUrl">URL</param>

        /// <returns></returns>

        private static string GetStringByUrl(string strUrl)

        {

            //与指定URL创建HTTP请求

            WebRequest wrt = WebRequest.Create(strUrl);

            //获取对应HTTP恳求的响应

            WebResponse wrse = wrt.GetResponse();

            //获取响应流

            Stream strM = wrse.GetResponseStream();

            //对接响应流(以"GBK"字符集)

            StreamReader SR = new StreamReader(strM, Encoding.GetEncoding("UTF-8"));

            //获取响应流的全部字符串

            string strallstrm = SR.ReadToEnd();

            //封闭读取流

            SR.Close();



            //返回网页html代码

            return strallstrm;

        }

        /// <summary>

        /// 传入URL返回网页的html代码

        /// 这个方法在这里没有用到

        /// </summary>

        /// <param name="Url">URL</param>

        /// <returns></returns>

        public string GetUrltoHtml(string Url)

        {

            StringBuilder content = new StringBuilder();



            try

            {

                // 与指定URL创建HTTP请求

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

                request.KeepAlive = false;

                // 获取对应HTTP请求的响应

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // 获取响应流

                Stream responseStream = response.GetResponseStream();

                // 对接响应流(以"GBK"字符集)

                StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));

                // 开始读取数据

                Char[] sReaderBuffer = new Char[256];

                int count = sReader.Read(sReaderBuffer, 0, 256);

                while (count > 0)

                {

                    String tempStr = new String(sReaderBuffer, 0, count);

                    content.Append(tempStr);

                    count = sReader.Read(sReaderBuffer, 0, 256);

                }

                // 读取结束

                sReader.Close();

            }

            catch (Exception)

            {

                content = new StringBuilder("Runtime Error");

            }



            return content.ToString();



        }



       /// <summary>

       /// 符合下列规则也可使用

        /// 返回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)

        {

            string  str = "";

            try

            {

                XmlDocument xml = new XmlDocument();

                // xml.LoadXml("<?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))

                    {



                        str=nodes[0].InnerText+","+ nodes[2].InnerText +","+ nodes[3].InnerText+","+nodes[6].InnerText +","+nodes[7].InnerText+","+ nodes[4].InnerText+","+ nodes[5].InnerText ;

                    }

                }

                return str.ToString();

            }

            catch

            {

                return str.ToString();

            }

        }

       



        /// <summary>

        /// 查询号码归属地

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnQuery_Click(object sender, EventArgs e)

        {

            ServiceMobile.MobileSoapClient mc = new ServiceMobile.MobileSoapClient();



            string Mobile = txtmob.Text.Trim().ToString();

            string Province = null;

            string City = null;

            string AreaCode = null;

            string PostCode = null;

            string Corp = null;

            string Card = null;



           

            if (string.IsNullOrEmpty(txtmob.Text))

            {

                MessageBox.Show("请输入号码!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;

            }

            else

            {

                bool result = mc.Query(Mobile, out  Province, out  City, out  AreaCode, out  PostCode, out  Corp, out  Card);



                if (result == true)

                {

                    txtMobile.Text = Mobile;

                    txtProvince.Text = Province;

                    txtCity.Text = City;

                    txtAreaCode.Text = AreaCode;

                    txtPostCode.Text = PostCode;

                    txtCopy.Text = Corp;

                    txtCard.Text = Card;



                    richTextBox1.AppendText("Mobile:" + Mobile + "\r\n" + "Province:" + Province + "\r\n" + "City:" + City + "\r\n" + "AreaCode:" + AreaCode + "\r\n" + "PostCode:" + PostCode + "\r\n" + "Corp:" + Corp + "\r\n" + "Card:" + Card + "\r\n");

                    MessageBox.Show("查询成功!");

                }

                else if (result == false)

                {

                    MessageBox.Show("查询失败 服务器返回超时!");

                }



            }



            //string str  = GetInfoByxml("http://api.showji.com/Locating/? m={0}", txtm.Text.Trim());

            //string[] array = str.Split(',');

            //foreach (string item in array)

            //{

            //    richTextBox1.Text += "__" + item;

            //}



            //if (txtm.Text.Trim().ToString() != "")

            //{

            //    //抓取网页html代码

              //    htmlCode = GetStringByUrl("http://api.showji.com/locating/?m=" + txtm.Text.Trim().ToString());



          //    //查询号码归属地

            //    QueryLocating(htmlCode);

            //}

            //else

            //{

            //    txtMobile.Text = "";

            //    txtProvince.Text = "";

            //    txtCity.Text = "";

            //    txtAreaCode.Text = "";

            //    txtPostCode.Text = "";

            //    txtCard.Text = "";

            //    MessageBox.Show("请输入号码!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}



        }

       

         ///返回 bool 值,表示用户输入的号码是否可识别。

          /// <summary>

         /// 查询手机号所属地方法

          /// </summary>

         /// <param name="Mobile">查询的手机号码</param>

         /// <param name="Province">返回手机号码归属地的省份</param>

         /// <param name="City">返回手机号码归属地的城市</param>

         /// <param name="AreaCode">返回手机号码归属地的区号</param>

         /// <param name="PostCode">返回手机号码归属地的邮编</param>

         /// <param name="Corp">返回所属运营商</param>

         /// <param name="Card">返回所属卡类型</param>

         /// <returns></returns>

      private  bool Query(string Mobile, out string Province, out string City, out string AreaCode, out

string PostCode, out string Corp, out string Card)

    {

        bool result = mc.Query(Mobile, out  Province, out  City, out  AreaCode, out  PostCode, out  Corp, out  Card);

         return result;

    }

    }

}

 

你可能感兴趣的:(获取)