How to get domain name from Given IP in C#?

后端 未结 3 871
北海茫月
北海茫月 2020-12-03 17:54

I want to get domain name from a given IP. E.g If I give IP as \"172.24.17.85\" I should get only domain name like my domain name is sonata.net.

Any code snippet for

相关标签:
3条回答
  • 2020-12-03 18:41
    Console.WriteLine("DomainName: {0}", Dns.GetHostEntry("1.1.1.1").HostName);
    
    0 讨论(0)
  • 2020-12-03 18:46

    Have you tried Dns.GetHostEntry?

    Example:

    using System;
    using System.Net;
    
    class Test
    {
        static void Main(string[] args)
        {
            IPAddress addr = IPAddress.Parse("69.59.196.211");
            IPHostEntry entry = Dns.GetHostEntry(addr);
            Console.WriteLine(entry.HostName); // Prints "stackoverflow.com"
        }
    }
    

    Note that this didn't work for the example you gave... if a reverse DNS lookup doesn't work, I'm not sure what you can do.

    0 讨论(0)
  • I really doubt if this is going to be possible. There could be n number of domains pointing to a single IP. You can do some research on Reverse DNS lookup.

    0 讨论(0)
提交回复
热议问题