C#获取本机公网IP

using System;
using System.Net;
using System.Text.RegularExpressions;
 
namespace GetInternetIP
{
    public class GetInternetIP
    {
        public static string GetIP()
        {
            using (var webClient = new WebClient())
            {
                try
                {
                    var temp = webClient.DownloadString("http://iframe.ip138.com/ic.asp");
                    return Regex.Match(temp, @"\[(?\d+\.\d+\.\d+\.\d+)]").Groups["ip"].Value;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
    }
}

你可能感兴趣的:(C#)