路漫漫,求索不息

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

查询手机号码归属地网址http://www.k780.com/api/Phone_Get

api查询:

 public string QueryPhoneAttribution(string mobile)
    {
        string xml = string.Empty;
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("http://api.k780.com/?app=phone.get&phone={0}&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml", mobile);
        try
        {
            //与指定URL创建HTTP请求
            HttpWebRequest request = HttpWebRequest.Create(sb.ToString()) as HttpWebRequest;
            request.Timeout = 5000;   //超时时间5秒
            request.ContentType = "application/x-www-form-urlencoded";
            //获取对应HTTP请求的响应
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            using (StreamReader streamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
            {
                xml = streamReader.ReadToEnd();
                streamReader.Close();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
                XmlNode isSuccess = doc.SelectSingleNode("/root/success");
                if (isSuccess != null && isSuccess.InnerText == "0")//失败
                {
                    string errorMessage = doc.SelectSingleNode("/root/msg").InnerText;
                    return errorMessage;
                }
                if (isSuccess != null && isSuccess.InnerText == "1")//成功
                {
                    XmlNode node = doc.SelectSingleNode("/root/result/att");
                    return node.InnerText;
                }
                return string.Empty;
            }
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

 

本地查询方法:

需要引用System.Data.SQLite.dll,同时要存在data文件(数据库)

public string LocalQueryPhoneAttribution(string phoneNum)
    {
        string phoneAttribution = "";
        string tel = "";
        tel = phoneNum.Substring(0, 3);
        phoneNum = phoneNum.Substring(0, 7);//取手机号码前面七位进行查询
        string sql = "select * from '" + tel + "' where numberrange='" + phoneNum + "' limit 0,1";
        SQLiteConnection con = null;
        SQLiteCommand cmd = null;
        SQLiteDataReader dr = null;

 

        Stopwatch sw = new Stopwatch();
        sw.Start();

        con = new SQLiteConnection("Data Source=D:\\Study\\练习\\asp.net网站\\WebSite1\\Bin\\Data");
        cmd = new SQLiteCommand(sql, con);
        con.Open();
        dr = cmd.ExecuteReader();
        DataTable dt = null;
        if (dr.HasRows)//如果存在此记录
        {
                      phoneAttribution = dr.GetValue(1).ToString();
        }
        sw.Start();
        var aa = sw.ElapsedMilliseconds;//监控执行这个方法的时间
        return phoneAttribution;
    }

posted on 2013-10-08 18:14  路漫漫,求索不息  阅读(593)  评论(0编辑  收藏  举报