您现在的位置是:网站首页> 编程资料编程资料

.net获取本机公网IP地址示例_实用技巧_

2023-05-24 394人已围观

简介 .net获取本机公网IP地址示例_实用技巧_

代码很简单,直接看代码

复制代码 代码如下:

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, @"\[(?\d+\.\d+\.\d+\.\d+)]").Groups["ip"].Value;
                    return !string.IsNullOrEmpty(ip) ? ip : null;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
    }
}

-六神源码网