思路构建

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

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

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

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

  1. 要引用dll Newtonsoft.Json.dll

  2. 下载地址:http://www.newtonsoft.com/products/json/

  3. 1.解压下载文件,得到Newtonsoft.Json.dll
     2.在项目中添加引用..
    3.兼容.net framwork2.04.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

调用方式:

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

根据Ip获取城市帮助类的更多相关文章

  1. php 根据ip获取城市以及网络运营商名称(利用qqwry.dat)

    根据用户IP地址判定出所在城市以及网络运营商 qqwry.dat下载地址:http://files.cnblogs.com/guangxiaoluo/qqwry.rar  解压出来即可 //获取用户真 ...

  2. c#根据ip获取城市地址

    用的API是百度.新浪.淘宝: 1.首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined. public stati ...

  3. 通过外部接口 根据ip获取城市名

    3种接口 淘宝/百度/不知名/   推荐淘宝接口 ip自个去获取,下附带php 获取ip的示例 function getIP() { static $realip; if (isset($_SERVE ...

  4. Spark- 根据IP获取城市(java)

    开源 IP 地址定位库 ip2region 1.4 ip2region 是准确率 99.9% 的 IP 地址定位库,0.0x毫秒级查询,数据库文件大小只有 2.7M,提供了 Java.PHP.C.Py ...

  5. C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

    百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...

  6. 百度地图API的IP定位城市和浏览器定位(转)

    百度地图API提供了Geolocation 和 LocalCity两个服务类. 这俩API可以分别供用户在JavaScript中进行定位和城市确认. 1 本质上,Geolocation这个类是使用了支 ...

  7. 根据IP获得城市信息(百度API的运用)

    /**      * 根据IP获取城市      * @param string $ip ip地址      * @return array      * http://api.map.baidu.c ...

  8. 根据IP地址获取地址所在城市帮助类(IPHelper)

    很多类库都是需要在长时间的编写过程中进行积累的,进入软件编程行业已经是第五个年头了,从2011年写下第一行代码到现在不知道已经写了多少行代码了,时间也过得挺快的.最近事情比较多,也很少写博客了,最近项 ...

  9. 根据IP获取所在的国家城市

    根据IP获取所在的国家城市 新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:htt ...

随机推荐

  1. eclipse快速收缩展开代码

    快速折叠所有代码和展开所有代码都是CTRL+SHIFT+/

  2. go mobile 得生命周期事件

    生命周期事件,就是状态从一个阶段切换成另外一个状态时触发的事件.所以我们可以看到 lifecycle.Event 的定义如下:   生命周期一共有下面四个阶段: lifecycle.StageDead ...

  3. 算法库:OpenCV3编译配置

    2016-01-20  23:55 更新: 关于Opencv3.1的lib文件 opencv_aruco310d.libopencv_bgsegm310d.libopencv_bioinspired3 ...

  4. ecmall数据字典

    ecm_acategory //文章分类表 字段 类型 Null 默认 注释 cate_id int(10) 否 自增ID号,分类ID号 cate_name varchar(100) 否 分类的名称 ...

  5. 1314: ZZY的困惑

    1314: ZZY的困惑 Time Limit: 2 Sec  Memory Limit: 128 M[Submit][Status][Web Board] Description ZZY有很多爱好~ ...

  6. python 简单实现文件拷贝

    1.背景 一日加班需要写一个文件拷贝的函数. 写了几版拷贝函数,有需要的直接粘贴过去 def CopyLocaleFile1(sorfile,desfile): #第一版 sorfp=open(sor ...

  7. 测试一个域名DNS查询时间的shell脚本

    脚本内容: #!/bin/bash #目标域名 site=${site:-www.ptesting.com} for((i=1;i<=10000;i++)) do     #COUNTER='e ...

  8. 【翻译习作】 Windows Workflow Foundation程序开发-前言

    Windows Workflow Foundation程序开发-基于XAML和C#的WF实战技术与例程 ——C#程序员的WF功能与编程接口技术指导 前言 Windows Workflow Founda ...

  9. 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView

    本文面向Android初级开发者,有一定的Java和Android知识即可. 文章覆盖知识点:HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新List ...

  10. gem5 运行x86全系统仿真

    使用gem5可以启动Linux内核,称为全系统模拟,启动之后,可以通过telent连接,进行访问,但四telent有时不稳定,gem5推荐使用m5term进行连接访问,整个步骤如下: (1)打开终端, ...