思路构建

1.先通过本地的测IP地址库进行匹配

2.如果本地IP地址库存在此IP的城市信息,就直接返回,调用速度也快

3.如果本地没有对应的IP城市信息,必须通过调用网络的IP查询的API了,这里我使用了(百度,新浪,和淘宝)

注意:百度的调用不是很正常,大批量调用

 要引用dll Newtonsoft.Json.dll
 下载地址:http://www.newtonsoft.com/products/json/
 1.解压下载文件,得到Newtonsoft.Json.dll 2.在项目中添加引用.. 3.兼容.net framwork2.0到4.5框架

使用NewtonSoft.JSON.dll来序列化和发序列化对象

1        #region 调用百度  新浪和淘宝接口返回地址   1.百度 2.新浪 3.淘宝
2         /// <summary>
3         /// 调用百度  新浪和淘宝接口返回地址   1.百度 2.新浪 3.淘宝
4         /// </summary>
5         /// <param name="Serve">1.百度 2.新浪 3.淘宝</param>
6         /// <param name="ipAddress"></param>
7         /// <returns></returns>
8         public string GetAddress(int Serve, string ipAddress)
9         {
10             try
11             {
12                 var match =
13                     new Regex(@"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))");
14                 if (!match.IsMatch(ipAddress))
15                 {
16                     return string.Empty;
17                 }
18                 var webClient = new System.Net.WebClient();
19                 NameValueCollection postValues = null;
20                 // 向服务器发送POST数据
21                 var url = string.Empty;
22                 if (Serve == 1)
23                 {
24                     url = "http://api.map.baidu.com/location/ip";
25                     postValues = new System.Collections.Specialized.NameValueCollection
26                             {
27                                 {"ak", "MRkBd6jnGOf8O5F58KKrvit5"},
28                                 {"ip", ipAddress},
29                                 {"coor", "bd09ll"}
30                             };
31                 }
32                 else if (Serve == 2)
33                 {
34                     url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php";
35                     postValues = new System.Collections.Specialized.NameValueCollection
36                             {
37                                 {"format", "json"},
38                                 {"ip", ipAddress}
39                             };
40                 }
41                 else
42                 {
43                     url = "http://ip.taobao.com/service/getIpInfo.php";
44                     postValues = new System.Collections.Specialized.NameValueCollection
45                             {
46                                 {"ip", ipAddress}
47                             };
48                 }
49
50                 byte[] responseArray = webClient.UploadValues(url, postValues);
51
52                 string response = System.Text.Encoding.UTF8.GetString(responseArray);
53
54                 var dataJson = JObject.Parse(response);  //动态解析  正常的解析无法生效
55                 string address = string.Empty;
56                 //百度接口
57                 if (Serve == 1)
58                 {
59                     if (dataJson["status"].ToString() == "0")
60                     {
61                         address = dataJson["content"]["address_detail"]["province"] + "," + dataJson["content"]["address_detail"]["city"];
62                     }
63                 }
64                 //新浪接口
65                 else if (Serve == 2)
66                 {
67                     if (dataJson["ret"].ToString() == "1")
68                     {
69                         address = dataJson["province"] + "," + dataJson["city"];
70                     }
71                 }
72                 //淘宝接口
73                 else
74                 {
75                     if (dataJson["code"].ToString() == "0")
76                     {
77                         if (!string.IsNullOrEmpty(dataJson["data"]["region"].ToString()))
78                             address = dataJson["data"]["region"] + "," + dataJson["data"]["city"];
79                     }
80                 }
81                 if (string.IsNullOrEmpty(address))
82                 {
83                     address = "局域网";
84                 }
85                 return address;
86             }
87             catch(Exception ex)
88             {
89                 Console.WriteLine(ex.Message);
90                 return string.Empty;
91             }
92         }
93         #endregion

调用方式:

ipAddressName = ipHelper.GetAddress(2, ipaddress);//调用新浪接口返回数据

转载于:https://www.cnblogs.com/zhangsupermaker/p/4550171.html

根据Ip获取城市帮助类相关推荐

  1. php免费根据ip查城市,根据ip获取城市的方法

    这个方法放到includes/lib_base.php的最下面 /* * * 根据ip获取城市的方法 * @param type $ip ip地址 * @return boolean|string * ...

  2. 根据IP获取城市-新浪ip接口

    /**+----------------------------------------------------------* content : 获取IP地址* time : 2018年7月9日+- ...

  3. Java 根据IP获取城市(ip2region)

    简介: 1.疫情期间,大家注意防护哈 2.今天看到了抖音和快手的新功能,可以看到IP属性,于是灵感立马闪现,出于好奇就打开电脑埋头苦研,去查各种资料. Ip2region是什么? ip2region ...

  4. php根据城市获取天气预报,根据浏览者ip获取城市,然后在获取城市天气预报

    /** * 根据经纬度获取城市 * @param unknown $point * @return mixed */ public function getCity() { $ip = $_SERVE ...

  5. python 通过ip获取城市_Python根据用户IP判断所属城市 !

    事情源于7月以来,SEO销售额下降比较明显,老板让我想想办法提升SEO销售额. 而销量=流量×转化率×客单价,客单价我这边没法控制,只能从流量和转化率两个方向入手了.但对SEO来讲,短时间大幅度提升S ...

  6. python 通过ip获取城市_python中通过客户端IP拿到所在城市和当地天气信息—附带项目案例...

    首先,需求分析: 1.拿到客户端IP 2.通过IP拿到客户端所在地区 3.通过地区拿到当地天气 4.整合功能,展示给用户 第一步,如何拿到用户IP 我们以Django环境为例 # 客户端的请求,IP信 ...

  7. python 通过ip获取城市_python shell根据ip获取主机名代码示例

    这篇文章里我们主要分享了python中shell 根据 ip 获取 hostname 或根据 hostname 获取 ip的代码,具体介绍如下. 笔者有时候需要根据hostname获取ip 比如根据m ...

  8. python 通过ip获取城市_python 根据ip获取地理位置

    !/usr/bin/python coding=utf-8 import dpkt import socket import pygeoip import optparse gi = pygeoip. ...

  9. 关于调用淘宝IP获取城市信息-- 随笔

    首先获取客户端的IP地址 function getIPaddress(){$IPaddress='';if (isset($_SERVER)){if (isset($_SERVER["HTT ...

最新文章

  1. FineUI小技巧(5)向子窗口传值,向父窗口传值
  2. P4879-ycz的妹子【分块】
  3. 【爬虫】爬取带有cookie才能获取网页内容的新闻网站
  4. 人工智能 - paddlepaddle飞桨 - 入门之安装教程
  5. 大型情感剧集Selenium:1_介绍 #华为云·寻找黑马程序员#
  6. word多级编号,如何让第一级为大写“一”,其他级别均为小写1.
  7. 标准工时分析软件VIOOVI,适配现代化工业的人工智能软件
  8. df.to_csv输出结果隔行多一个空行的解决办法
  9. X86、X86_64、X64的区别
  10. C语言麻将递归,一个简洁明了的递归函数——判断[麻将]的和牌
  11. Daimayuan Online Judge 上帝的集合
  12. 普元框架-那些年一起走过的坑
  13. linux驱动21:内核创建类class_create
  14. 程序逸的Java项目之旅-图书管理系统之验证码和用户数据的后台获取(1)
  15. USB设备无法识别也无法读取怎么办?
  16. 从0到一开发微信小程序(6)—小程序常用API
  17. 重磅!中国商业联合会专家委员会商贸物流与供应链智库成立在即
  18. c basic library framework - simplec 2.0.0
  19. 旅行照片剪辑--苏州同里篇
  20. 关系数据库理论----如何判断关系模式规范化达到第几范式

热门文章

  1. OpenCV3学习(12.3) kalman滤波器
  2. OpenCV3学习(12.2) 目标跟踪之运动模板法segmentMotion实现(Mat版本)
  3. 7-5 素数判断 (10 分)
  4. python随机列表文本_在python中从单词列表返回随机单词
  5. Cannot find module 'less-bundle-promise'
  6. element-UI ,Table组件实现拖拽效果
  7. 实验三:shell编程(1)
  8. 《转》 在C++中使用TinyXML2解析xml
  9. 存储优化 - 删除重复记录只保留单条
  10. C++之第一个程序Hello World,使用IO库输出Hello World