柔城

SOSOFT articles

.net获取本机公网IP代码

类的代码如下:

 1 using System;
 2 using System.Net;
 3 using System.Text.RegularExpressions;
 4 
 5 namespace Keleyi.Com
 6 {
 7     public class GetInternetIP
 8     {
 9         public static string GetIP()
10         {
11             using (var webClient = new WebClient())
12             {
13                 try
14                 {
15                     var temp = webClient.DownloadString("http://iframe.ip138.com/ic.asp");
16                     var ip = Regex.Match(temp, @"\[(?<ip>\d+\.\d+\.\d+\.\d+)]").Groups["ip"].Value;
17                     return !string.IsNullOrEmpty(ip) ? ip : null;
18                 }
19                 catch (Exception ex)
20                 {
21                     return ex.Message;
22                 }
23             }
24         }
25     }
26 }

 http://www.cnblogs.com/sosoft/

posted on 2014-01-04 21:37  柔城  阅读(2321)  评论(0编辑  收藏  举报

导航