C#从网址中提取域名地址和获取域名解析的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.GetHostByName(Result);
                IPAddress ip = host.AddressList[0];
                rIP = ip.ToString();
            }
            catch
            {
                rIP = "请输入正确的域名,或者您的电脑没有联互联网";
            }


            return rIP;
        }

评论