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
Release: 2025-01-18 11:22:09
Original
813 people have browsed it

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>
Copy after login

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>
Copy after login

Command-Line Solutions

Command-line users have several choices:

  • On Linux and Windows:

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

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template