这是一个通过调用淘宝ip地址库实现ip地址查询的功能类
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace LixinCommon
7 {
8 /// <summary>
9 /// 淘宝IP地址库帮助类。
10 /// 提供查询ip地址信息功能。
11 /// 服务的主页:http://ip.taobao.com
12 /// 作者:lixin
13 /// 作者Email:lixin@lixin.me
14 /// 日期:2014年3月30日
15 /// 备注:为了保障服务正常运行,每个用户的访问频率需小于10qps。
16 /// </summary>
17 public class TaobaoIPHelper
18 {
19 /// <summary>
20 /// 查询ip信息
21 /// </summary>
22 /// <param name="ip"></param>
23 /// <returns></returns>
24 public static TaobaoIP GetIP(string ip)
25 {
26 System.Net.WebClient client = new System.Net.WebClient();
27 try
28 {
29 string data = client.DownloadString("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
30 //当code==1时,表示失败
31 if (data.IndexOf(""code":1") > -1)
32 {
33 return new TaobaoIP()
34 {
35 code = 1,
36 errorMsg = data
37 };
38 }
39 using (System.IO.MemoryStream mm = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(data)))
40 {
41 System.Runtime.Serialization.Json.DataContractJsonSerializer myJson = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(TaobaoIP));
42 TaobaoIP item = (TaobaoIP)myJson.ReadObject(mm);
43
44 return item;
45 }
46 }
47 catch (Exception ex)
48 {
49 return new TaobaoIP()
50 {
51 code = 1,
52 errorMsg = ex.Message,
53 data = new TaobaoIP_Data() { }
54 };
55 }
56 }
57 /// <summary>
58 /// 根据域名返回域名的ip信息
59 /// </summary>
60 /// <param name="domain"></param>
61 /// <returns></returns>
62 public static TaobaoIP[] GetDomain(string domain)
63 {
64 System.Net.IPAddress[] ips = System.Net.Dns.GetHostAddresses(domain);
65 TaobaoIP[] result = new TaobaoIP[ips.Length];
66 for (int i = 0, j = ips.Length; i < j; i++)
67 {
68 result[i] = GetIP(ips[i].ToString());
69 }
70 return result;
71 }
72 }
73 public struct TaobaoIP
74 {
75 /// <summary>
76 /// 0:成功;1:失败
77 /// </summary>
78 public int code;
79 /// <summary>
80 /// ip响应信息
81 /// </summary>
82 public TaobaoIP_Data data;
83 /// <summary>
84 /// 错误信息
85 /// </summary>
86 [NonSerialized]
87 public string errorMsg;
88 }
89 public struct TaobaoIP_Data
90 {
91 /// <summary>
92 /// ip地址
93 /// </summary>
94 public string ip;
95 /// <summary>
96 /// 国家
97 /// </summary>
98 public string country;
99 /// <summary>
100 /// 区域
101 /// </summary>
102 public string area;
103 /// <summary>
104 /// 省份
105 /// </summary>
106 public string region;
107 /// <summary>
108 /// 城市
109 /// </summary>
110 public string city;
111 /// <summary>
112 /// 运营商
113 /// </summary>
114 public string isp;
115 public string country_id;
116 public string area_id;
117 public string region_id;
118 public string city_id;
119 public string county_id;
120 public string isp_id;
121 }
122 }
用法很简单,示例代码如下:
1 TaobaoIP result1 = TaobaoIPHelper.GetIP("8.8.8.8");
2 if (result1.code == 0)
3 {
4 Console.WriteLine(string.Format("调用成功。该ip的国家为{0},省份为{1},城市为{2},运营商为{3}"
5 , result1.data.country, result1.data.region, result1.data.city, result1.data.isp)
6 );
7 }
8 else
9 {
10 Console.WriteLine("失败,原因为:"+result1.errorMsg);
11 }
12 TaobaoIP[] result2 = TaobaoIPHelper.GetDomain("lixin.me");
13 foreach (TaobaoIP item in result2)
14 {
15 if (item.code == 0)
16 {
17 Console.WriteLine(string.Format("调用成功。该ip的国家为{0},省份为{1},城市为{2},运营商为{3}"
18 , item.data.country, item.data.region, item.data.city, item.data.isp)
19 );
20 }
21 else
22 {
23 Console.WriteLine("失败,原因为:" + item.errorMsg);
24 }
25 }