C# Dns.GetHostEntry() - exception: No such host is known

和自甴很熟 提交于 2021-02-11 16:41:02

问题


From documentation Dns.GetHostEntry() Resolves a host name or IP address to an IPHostEntry instance. Could anyone help me undestand why this function doesn't work for some external IP address (it works for internal IP address) and the ping command works normally?

My code is:

using System;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Please enter a host name or IP address");
                Console.WriteLine("Usage: ConsoleApplication1 8.8.8.8");
                return 1;
            }

            string addr = args[0];

            try
            {
                IPHostEntry host = Dns.GetHostEntry(addr);
                Console.WriteLine($"GetHostEntry({addr}) returns HostName: {host.HostName}");
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e);
            }

            return 0;
        }

    }
}

Output:

C:\Users\Administrator\Desktop>ping stackoverflow.com

Pinging stackoverflow.com [151.101.1.69] with 32 bytes of data:
Reply from 151.101.1.69: bytes=32 time=35ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57

Ping statistics for 151.101.1.69:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 16ms, Maximum = 35ms, Average = 20ms

C:\Users\Administrator\Desktop>ConsoleApplication1.exe 151.101.1.69
Exception: System.Net.Sockets.SocketException (0x80004005): No such host is known
   at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6)
   at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
   at ConsoleApplication1.Program.Main(String[] args)

C:\Users\Administrator\Desktop>ping 151.101.1.69

Pinging 151.101.1.69 with 32 bytes of data:
Reply from 151.101.1.69: bytes=32 time=27ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57
Reply from 151.101.1.69: bytes=32 time=17ms TTL=57

Ping statistics for 151.101.1.69:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 16ms, Maximum = 27ms, Average = 19ms

I found a solution changing the file

C:\Windows\System32\drivers\etc\hosts

Adding the line:

151.101.1.69 stackoverflow.com

It works after this change

C:\Users\Administrator\Desktop>ConsoleApplication1.exe 151.101.1.69
GetHostEntry(151.101.1.69) returns HostName: stackoverflow.com

In my opinion, changing hosts file is not a good solution to solve this issue. Does anyone know a better solution to solve this?

来源:https://stackoverflow.com/questions/59770306/c-sharp-dns-gethostentry-exception-no-such-host-is-known

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!