当前位置:Gxlcms > asp.net > .net获取本机公网IP地址示例

.net获取本机公网IP地址示例

时间:2021-07-01 10:21:17 帮助过:49人阅读

代码很简单,直接看代码
代码如下:
using System;
using System.Net;
using System.Text.RegularExpressions;

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

人气教程排行