两极距离 發表於 2021-4-8 16:02:00

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

<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">/// &lt;summary&gt;
      /// 通过域名获得域名解析的IP地址
      /// &lt;/summary&gt;
      /// &lt;param name="url"&gt;网址&lt;/param&gt;
      /// &lt;returns&gt;返回域名解析的IP地址&lt;/returns&gt;
      private string GetYuMingIP(string url) {
            string rIP = string.Empty;
            string p = @"(http|https)://(?&lt;domain&gt;[^(:|/]*)";
            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;
                rIP = ip.ToString();
            }
            catch {
                rIP = "请输入正确的域名,或者您的电脑没有联互联网";
            }

            return rIP;
      }
</pre>
</div>
<p>  </p><br><br>
来源:https://www.cnblogs.com/anduinlothar/p/14632735.html
頁: [1]
查看完整版本: 通过域名获得域名解析的IP地址