LOGO 首页 OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 技术文档 其他文档  
 
网站管理员

[点晴永久免费OA]C#根据ip获取城市地址

admin
2022年5月31日 11:4 本文热度 5623

用的API是百度、新浪、淘宝:

1、首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined。

1 public static bool HasChinese(string str)
2     {
3         return Regex.IsMatch(str, @"[u4e00-u9fa5]");
4     }

百度的API

/// <summary>
    /// 百度api
    /// </summary>
    /// <returns></returns>
    public static string GetBaiduIp(string ip)
    {
        try
        {
            string cs = "";
            string url = "http://api.map.baidu.com/location/ip?ak=GlfVlFKSc6Y7aSr73IHM3lQI&ip=" + ip;
            WebClient client = new WebClient();
            var buffer = client.DownloadData(url);
            string jsonText = Encoding.UTF8.GetString(buffer);
            JObject jo = JObject.Parse(jsonText);
            var txt = jo["content"]["address_detail"]["city"];
            JToken st = txt;
            string str = st.ToString();
            if (str == "")
            {
                cs = GetCS(ip);
                return cs;
            }
            int s = str.IndexOf('');
            string css = str.Substring(0, s);
            bool bl = HasChinese(css);
            if (bl)
            {
                cs = css;
            }
            else
            {
                cs = GetCS(ip);
            }
            return cs;
        }
        catch
        {
            return GetIPCitys(ip);
        }
    }

新浪API(这个是直接获取网页源代码,然后抓取)

 1  /// <summary>
 2     /// 新浪api
 3     /// </summary>
 4     /// <param name="ip"></param>
 5     /// <returns></returns>
 6 public static string GetCS(string ip)
 7     {
 8         try { 
 9         string url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip;
10         WebClient MyWebClient = new WebClient();
11         MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
12         Byte[] pageData = MyWebClient.DownloadData(url); //从指定网站下载数据
13         string stt = Encoding.GetEncoding("GBK").GetString(pageData).Trim();
14         return stt.Substring(stt.Length - 2, 2);
15         }
16         catch
17         {
18             return "未知";
19         }
20 
21     }

淘宝API

 1 /// <summary>
 2     /// 淘宝api
 3     /// </summary>
 4     /// <param name="strIP"></param>
 5     /// <returns></returns>
 6     public static string GetIPCitys(string strIP)
 7     {
 8         try
 9         {
10             string Url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + strIP + "";
11 
12             System.Net.WebRequest wReq = System.Net.WebRequest.create(Url);
13             wReq.Timeout = 2000;
14             System.Net.WebResponse wResp = wReq.GetResponse();
15             System.IO.Stream respStream = wResp.GetResponseStream();
16             using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream))
17             {
18                 string jsonText = reader.ReadToEnd();
19                 JObject ja = (JObject)JsonConvert.DeserializeObject(jsonText);
20                 if (ja["code"].ToString() == "0")
21                 {
22                     string c = ja["data"]["city"].ToString();
23                     int ci = c.IndexOf('');
24                     if (ci != -1)
25                     {
26                         c = c.Remove(ci, 1);
27                     }
28                     return c;
29                 }
30                 else
31                 {
32                     return "未知";
33                 }
34             }
35         }
36         catch (Exception)
37         {
38             return ("未知");
39         }
40     }

该文章在 2022/5/31 11:04:36 编辑过

全部评论1

admin
2022年5月31日 11:15
ip-api.com接口(解析 json需要引入Newtonsoft.Json.dll ):1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39/// <summary> /// 根据IP 获取物理地址 /// </summary> /// <param name=ip>Ip地址</param> /// <returns></returns> public static string GetIpAddress(string ip) catch (Exception) finally return result; }126.net接口:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39/// <summary> /// 根据IP 获取物理地址 /// </summary> /// <param name=ip>Ip地址</param> /// <returns></returns> public static string GetstringIpAddress(string ip) string html = sr.ReadToEnd(); string pattern = ; Regex regex = new Regex(pattern, RegexOptions.None); Match match = regex.Match(html); string city=match.Groups[key1].Value; string province=match.Groups[key2].Value; result = city.Equals(province) ? city : (province + city); } catch (Exception) finally return result; }
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2026 ClickSun All Rights Reserved  粤ICP备13012886号-2  粤公网安备44030602007207号