通过域名获得域名解析的IP地址

/// <summary>
        /// 通过域名获得域名解析的IP地址
        /// </summary>
        /// <param name="url">网址</param>
        /// <returns>返回域名解析的IP地址</returns>
        private string GetYuMingIP(string url) {
            string rIP = string.Empty;
            string p = @"(http|https)://(?<domain>[^(:|/]*)";
            Regex reg = new Regex(p, RegexOptions.IgnoreCase);
            string ipAddress = url;
            if (!ipAddress.Contains("http")) {
                ipAddress = "http://" + ipAddress;
            }

            Match m = reg.Match(ipAddress);
            string Result = m.Groups["domain"].Value;//域名地址   如http://wwww.luofenmng.com/index.aspx  提取出来的是www.luofenming.com

            //以下是获取域名解析的IP地址
            try {
                IPHostEntry host = Dns.GetHostEntry(Result);
                IPAddress ip = host.AddressList[0];
                rIP = ip.ToString();
            }
            catch {
                rIP = "请输入正确的域名,或者您的电脑没有联互联网";
            }

            return rIP;
        }

  

posted @ 2021-04-08 16:02  安度  阅读(1135)  评论(0编辑  收藏  举报