C#使用HttpClient获取公网IP
C#使用HttpClient获取公网IP
C# 全选
/// <summary>
/// 获取公网IP
/// </summary>
/// <returns></returns>
public static async Task<string> GetNetIP()
{
string url = "https://api64.ipify.org/";
string ip = "";
try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
ip = await response.Content.ReadAsStringAsync();
}
}
catch (Exception e)
{
Console.WriteLine("获取IP地址发生错误: " + e.Message);
}
return ip;
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网