How to get the IP address of the server on which my C# application is running on?

前端 未结 26 2241
天命终不由人
天命终不由人 2020-11-22 06:01

I am running a server, and I want to display my own IP address.

What is the syntax for getting the computer\'s own (if possible, external) IP address?

Someon

26条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 06:28

    Yet another way to get your public IP address is to use OpenDNS's resolve1.opendns.com server with myip.opendns.com as the request.

    On the command line this is:

      nslookup myip.opendns.com resolver1.opendns.com
    

    Or in C# using the DNSClient nuget:

      var lookup = new LookupClient(new IPAddress(new byte[] { 208, 67, 222, 222 }));
      var result = lookup.Query("myip.opendns.com", QueryType.ANY);
    

    This is a bit cleaner than hitting http endpoints and parsing responses.

提交回复
热议问题