Home >Backend Development >C++ >How Can I Access My Router's Public IP Address?

How Can I Access My Router's Public IP Address?

Linda Hamilton
Linda HamiltonOriginal
2025-01-18 11:22:09817browse

How Can I Access My Router's Public IP Address?

Locating Your Router's Public IP Address

Determining your router's public IP address might seem tricky, but several methods make it straightforward.

Retrieving the IP via HTTP Request

Using C#, you can leverage HTTPClient to obtain your public IP:

<code class="language-csharp">public static async Task<IPAddress> GetExternalIpAddress()
{
    string externalIpString = (await new HttpClient().GetStringAsync("http://icanhazip.com"))
        .Replace("\r\n", "").Replace("\n", "").Trim();
    if (!IPAddress.TryParse(externalIpString, out IPAddress ipAddress)) return null;
    return ipAddress;
}</code>

Another option using WebClient:

<code class="language-csharp">public static void Main(string[] args)
{
    string externalIpString = new WebClient().DownloadString("http://icanhazip.com").Replace("\r\n", "").Replace("\n", "").Trim();
    IPAddress externalIp = IPAddress.Parse(externalIpString);

    Console.WriteLine(externalIp.ToString());
}</code>

Command-Line Solutions

Command-line users have several choices:

  • On Linux and Windows:

    <code class="language-bash">  wget -qO- http://bot.whatismyipaddress.com</code>
  • Using Curl:

    <code class="language-bash">  curl http://ipinfo.io/ip</code>

The above is the detailed content of How Can I Access My Router's Public IP Address?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn