首页 > 其他 > 详细

IP地址解析

时间:2014-04-14 23:12:11      阅读:576      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections;

namespace getipaddress
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient wc = new WebClient();
            string html = wc.DownloadString("http://iframe.ip138.com/ic.asp");//获取本机外网IP
            int iBegin = html.IndexOf([) + 1;
            int iEnd = html.IndexOf(]);
            string strIP = html.Substring(iBegin, iEnd - iBegin);

            html = wc.DownloadString(string.Format("http://ip.taobao.com/service/getIpInfo.php?ip={0}",strIP));//获得IP地址对应的信息
            html = ConvertUnicode2Chinese(html);
            Console.WriteLine(html);
            Hashtable ht = new Hashtable();
            GetInfo(html,ht);

        }
        /// <summary>
        /// 将编码转换成字符
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private static string ConvertUnicode2Chinese(string result)
        {
            Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            return reg.Replace(result,delegate(Match m){ return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
        }

        /// <summary>
        /// 将获得的JSON数据存放在Hashtable中
        /// </summary>
        /// <param name="source"></param>
        /// <param name="ht"></param>
        private static void GetInfo(string source,Hashtable ht)
        {
            Regex reg = new Regex("(?<=\")\\w*(?=\")|(?<=\")(\\d{1,3}\\.){3}\\d{1,3}(?=\")|(?<=\")-\\d*(?=\")");
            MatchCollection result = reg.Matches(source);
            int i = 0;
            object Key="";
            foreach (Group item in result)
            {
                if (i == 0)
                {
                    ht.Add(item.Value, "");
                    Key = item.Value;
                    i++;
                }
                else
                {
                    ht[Key] = item.Value;
                    i--;
                }
            }
            Console.ReadKey();
        }
    }

}
bubuko.com,布布扣

 

IP地址解析,布布扣,bubuko.com

IP地址解析

原文:http://www.cnblogs.com/having/p/3663389.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!