1

Trying to get the public address(not local IP) of the machine using c#. Unable to get Public IP.

We can get public IP Address using external libraries or API like in this link.

Is there any possible to get machine pubic IP address in c# without using external API and libraries?

christo
  • 129
  • 1
  • 2
  • 13
  • Make a call to [ipify](https://www.ipify.org/). Otherwise: open the Router web interface, dump the config, parse, find the external interface, copy the IP Address (unless the Router gives you this information through an API). If the Router is yours. I mean. – Jimi Nov 07 '18 at 06:13
  • Possible duplicate of https://stackoverflow.com/questions/3253701/get-public-external-ip-address – anangupadhyaya Nov 08 '18 at 09:16

2 Answers2

0

Try this

public String getPublicIp()
{
    HTTPGet req = new HTTPGet();
    req.Request("http://checkip.dyndns.org");
    string[] a = req.ResponseBody.Split(':');
    string a2 = a[1].Substring(1);
    string[] a3=a2.Split('<');
    string ip = a3[0];
    return ip;
}
0

Retrieve public IP (V4) address, asynchronously (recommended)

HttpClient client = new HttpClient();
var ipTask = client.GetStringAsync("https://api.ipify.org");
var ipAddress = await ipTask;

For IP V6, use this URL:

var ipTask = client.GetStringAsync("https://api6.ipify.org");
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77