本程序的原理是,用程序打开以下地址,然后从网页源码中提取出IP或域名对应的物理地址即可

http://www.ip138.com/ips1388.asp?ip=www.baidu.cn&action=2

效果截图如下:

C#之IP地址查看器..._string

C#之IP地址查看器..._action_02




主要代码如下:

//获取IP或域名地址函数
        public String getIpAddr(String ip)
        {
            if (ip.StartsWith("http://"))
                ip = ip.Replace("http://", "");
            String IP = "http://www.ip138.com/ips1388.asp?ip=";
            IP += ip;
            IP += "&action=2";
            String ipAddr = "";

            //获取网页源码
            System.Net.WebClient webClient = new System.Net.WebClient();
            String strSource = "";
            try
            {
                strSource =webClient.DownloadString(IP);
                //this.txbAddr.Text = strSource;
            }
            catch(System.Net.WebException e)
            {
                return ipAddr=e.ToString();
            }

            //提取地址
            String regex = @"<li>.+<li>";
            ipAddr = System.Text.RegularExpressions.Regex.Match(strSource, regex).ToString();
            ipAddr=ipAddr.Replace("<li>本站主数据:", "");
            ipAddr = ipAddr.Replace("</li><li>", "");
            return ipAddr;
        }