class Program
{
static void Main(string[] args)
{
var getIpInfoUrl = "http://ip.taobao.com/service/getIpInfo.php?ip={0}";
getIpInfoUrl = string.Format(getIpInfoUrl, "125.36.12.14");
var client = new WebClient();
var result = client.DownloadString(getIpInfoUrl);
var ipInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<IpInfo>(result);
foreach (var key in ipInfo.data.Keys)
Console.WriteLine("{0}:{1}", key, ipInfo.data[key]);
Console.Read();
}
}
public class IpInfo
{
public string code;
public Hashtable data;
}
第二种方法:
protected void Page_Load(object sender, EventArgs e)
{
string path = "http://ip.taobao.com/service/getIpInfo.php?ip=125.36.12.14";
HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(path);
HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse();
Stream stream = null;
try
{
stream = hwrp.GetResponseStream();
DataContractJsonSerializer datajson = new DataContractJsonSerializer(typeof(IpInfo));
IpInfo ipinfo = datajson.ReadObject(stream) as IpInfo;
Response.Write(ipinfo.code + "==" + ipinfo.data.ip + "==" + ipinfo.data.country + "==" + ipinfo.data.area);
}
finally
{
if (stream != null)
{
stream.Close();
stream.Dispose();
}
}
}
[DataContract]
public class IpInfo
{
[DataMember]
public int code;
[DataMember]
public data data;
}
[DataContract]
public class data
{
[DataMember]
public string ip;
[DataMember]
public string country;
[DataMember]
public string country_id;
[DataMember]
public string area;
[DataMember]
public string area_id;
[DataMember]
public string region;
[DataMember]
public string region_id;
[DataMember]
public string city;
[DataMember]
public string city_id;
[DataMember]
public string county;
[DataMember]
public string county_id;
[DataMember]
public string isp;
[DataMember]
public string isp_id;
}