读取ip 判断

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;
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值