码迷,mamicode.com
首页 > Web开发 > 详细

.net获取本机公网IP代码

时间:2015-12-31 08:51:37      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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;
                }
            }
        }
    }
}

  

.net获取本机公网IP代码

标签:

原文地址:http://www.cnblogs.com/shouce/p/5090573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!