网络DNS域名转换成IP地址(完整代码,测试通过)


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

namespace DNS2IP

{

    class Program

    {

        // 主函数,入口函数

        static void Main(string[] args)

        {

            string strDNS="​​www.google.com​​";

            string strIP = GetIP(strDNS);

            Console.WriteLine(strIP);

            Console.ReadLine();

        }

        // 利用域名,获取IP地址

        public static string GetIP(string strDNS)

        {

            IPHostEntry hostEntry = Dns.GetHostEntry(strDNS);

            IPEndPoint ipEndPoint = new IPEndPoint(hostEntry.AddressList[0], 0);

            string ipAddress = ipEndPoint.Address.ToString();

            return ipAddress;

        }

    }

}


运行界面: