C# and .NET examples · HTTP and integration
Get Your Public IP Address in C# with HttpClient
A minimal .NET 6 console application that shows how to get your public IP address in C#: it calls the ipify API with HttpClient and prints the result.
In this guide: Get started · Walk through the example · Understand the details · Check the limits
🚀 Quick start
Build and Run
cd PublicIP
dotnet run
📦 Prerequisites and installation
Requirements
- .NET 6 SDK plus a compatible .NET 6 runtime
- An active internet connection
💡 Usage
How It Works
The application sends an HTTP GET request to http://api.ipify.org, which returns your public IPv4 address as plain text. The result is printed to the console. If the request fails, the error message is printed instead.
Example Output
Your public IP Address: 203.0.113.42
🏗️ Architecture
Project Structure
PublicIP/
├── Program.cs # Fetches and prints the public IP address
├── PublicIP.csproj
└── PublicIP.sln
🧪 Development and testing
If you choose to run the network example, verify either an address message or a handled error, then supply console input to exit. This documentation review inspected the URL and error handler without contacting the external service.
🛠️ Troubleshooting and limitations
Notes
- The request uses plain
http://api.ipify.org. ipify also offershttps://api.ipify.org, andhttps://api64.ipify.orgfor IPv6 where available. - The code creates one
HttpClientfor a single request, which is fine for a console demo. In long-running applications, reuseHttpClientor useIHttpClientFactory.
The source uses plain HTTP and depends on an external service. Network or proxy failures are caught and printed; there is no retry policy or caller-provided cancellation token. After the request, Console.Read waits for input. The example IP address is illustrative, not a captured network result.
🔗 Related documentation
Related Examples
Documentation links
Read all C# and .NET Code Examples guides online · Project overview · All developer projects