應用淘寶ip地址庫查ip的示例。本站提示廣大學習愛好者:(應用淘寶ip地址庫查ip的示例)文章只能為提供參考,不一定能成為您想要的結果。以下是應用淘寶ip地址庫查ip的示例正文
應用淘寶ip地址庫的api查詢ip地址信息。
TaobaoIPHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LixinCommon
{
/// <summary>
/// 淘寶IP地址庫贊助類。
/// 供給查詢ip地址信息功效。
/// 辦事的主頁:http://ip.taobao.com
/// 作者:lixin
/// 作者Email:[email protected]
/// 日期:2014年3月30日
/// 備注:為了保證辦事正常運轉,每一個用戶的拜訪頻率需小於10qps。
/// </summary>
public class TaobaoIPHelper
{
/// <summary>
/// 查詢ip信息
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static TaobaoIP GetIP(string ip)
{
System.Net.WebClient client = new System.Net.WebClient();
try
{
string data = client.DownloadString("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
//當code==1時,表現掉敗
if (data.IndexOf("\"code\":1") > -1)
{
return new TaobaoIP()
{
code = 1,
errorMsg = data
};
}
using (System.IO.MemoryStream mm = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(data)))
{
System.Runtime.Serialization.Json.DataContractJsonSerializer myJson = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(TaobaoIP));
TaobaoIP item = (TaobaoIP)myJson.ReadObject(mm);
return item;
}
}
catch (Exception ex)
{
return new TaobaoIP()
{
code = 1,
errorMsg = ex.Message,
data = new TaobaoIP_Data() { }
};
}
}
/// <summary>
/// 依據域名前往域名的ip信息
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public static TaobaoIP[] GetDomain(string domain)
{
System.Net.IPAddress[] ips = System.Net.Dns.GetHostAddresses(domain);
TaobaoIP[] result = new TaobaoIP[ips.Length];
for (int i = 0, j = ips.Length; i < j; i++)
{
result[i] = GetIP(ips[i].ToString());
}
return result;
}
}
public struct TaobaoIP
{
/// <summary>
/// 0:勝利;1:掉敗
/// </summary>
public int code;
/// <summary>
/// ip呼應信息
/// </summary>
public TaobaoIP_Data data;
/// <summary>
/// 毛病信息
/// </summary>
[NonSerialized]
public string errorMsg;
}
public struct TaobaoIP_Data
{
/// <summary>
/// ip地址
/// </summary>
public string ip;
/// <summary>
/// 國度
/// </summary>
public string country;
/// <summary>
/// 區域
/// </summary>
public string area;
/// <summary>
/// 省分
/// </summary>
public string region;
/// <summary>
/// 城市
/// </summary>
public string city;
/// <summary>
/// 運營商
/// </summary>
public string isp;
public string country_id;
public string area_id;
public string region_id;
public string city_id;
public string county_id;
public string isp_id;
}
}
TaobaoIP result1 = TaobaoIPHelper.GetIP("8.8.8.8");
if (result1.code == 0)
{
Console.WriteLine(string.Format("挪用勝利。該ip的國度為{0},省分為{1},城市為{2},運營商為{3}"
, result1.data.country, result1.data.region, result1.data.city, result1.data.isp)
);
}
else
{
Console.WriteLine("掉敗,緣由為:"+result1.errorMsg);
}
TaobaoIP[] result2 = TaobaoIPHelper.GetDomain("lixin.me");
foreach (TaobaoIP item in result2)
{
if (item.code == 0)
{
Console.WriteLine(string.Format("挪用勝利。該ip的國度為{0},省分為{1},城市為{2},運營商為{3}"
, item.data.country, item.data.region, item.data.city, item.data.isp)
);
}
else
{
Console.WriteLine("掉敗,緣由為:" + item.errorMsg);
}
}