您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> asp.net编程 >> 正文

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

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 本文主要介绍了.net获取本机公网IP地址的方法,使用了ip138的数据,大家参考使用吧

代码很简单,直接看代码   代码如下: 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;                 }             }         }     } }