1 /// <summary> 2 /// 獲取本機全部ip地址 3 /// </summary> 4 /// <param name="netType">"InterNetwork":ipv4地址,"InterNetworkV6":ipv6地址</param> 5 /// <returns>ip地址集合</returns> 6 public static List<string> GetLocalIpAddress(string netType) 7 { 8 string hostName = Dns.GetHostName(); //獲取主機名稱 9 IPAddress[] addresses = Dns.GetHostAddresses(hostName); //解析主機IP地址 10 11 List<string> IPList = new List<string>(); 12 if (netType==string.Empty) 13 { 14 for (int i = 0; i < addresses.Length; i++) 15 { 16 IPList.Add(addresses[i].ToString()); 17 } 18 } 19 else 20 { 21 //AddressFamily.InterNetwork表示此IP爲IPv4, 22 //AddressFamily.InterNetworkV6表示此地址爲IPv6類型 23 for (int i = 0; i < addresses.Length; i++) 24 { 25 if (addresses[i].AddressFamily.ToString() == netType) 26 { 27 IPList.Add(addresses[i].ToString()); 28 } 29 } 30 } 31 return IPList; 32 }
1 List<string> ips= GetLocalIpAddress("");//獲取本地全部ip 2 List<string> ipv4_ips = GetLocalIpAddress("InterNetwork");//獲取ipv4類型的ip 3 List<string> ipv6_ips = GetLocalIpAddress("InterNetworkV6");//獲取ipv6類型的ip
若是本機在局域網內,直接調用上面的方法便可,以ipv4爲例:html
List<string> ipv4_ips = GetLocalIpAddress("InterNetwork");
若是本機只有一個網卡,且沒有安裝虛擬網卡,那麼ipv4_ips裏應該只有一條數據,即局域網內地址;網絡
List<string> ipv4_ips = GetLocalIpAddress("InterNetwork");
若是本機只有一個網卡,且沒有安裝虛擬網卡,那麼ipv4_ips裏應該只有一條數據,即公網IP地址;測試
步驟:先獲取到含有本機外網ip的頁面信息,再經過正則獲取到ip信息;網站
1 /// <summary> 2 /// 獲取頁面html 3 /// </summary> 4 /// <param name="url">請求的地址</param> 5 /// <param name="encoding">編碼方式</param> 6 /// <returns></returns> 7 public static string HttpGetPageHtml(string url, string encoding) 8 { 9 string pageHtml = string.Empty; 10 try 11 { 12 using (WebClient MyWebClient = new WebClient()) 13 { 14 Encoding encode = Encoding.GetEncoding(encoding); 15 MyWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36"); 16 MyWebClient.Credentials = CredentialCache.DefaultCredentials;//獲取或設置用於向Internet資源的請求進行身份驗證的網絡憑據 17 Byte[] pageData = MyWebClient.DownloadData(url); //從指定網站下載數據 18 pageHtml = encode.GetString(pageData); 19 } 20 } 21 catch (Exception e) 22 { 23 24 } 25 return pageHtml; 26 } 27 /// <summary> 28 /// 從html中經過正則找到ip信息(只支持ipv4地址) 29 /// </summary> 30 /// <param name="pageHtml"></param> 31 /// <returns></returns> 32 public static string GetIPFromHtml(String pageHtml) 33 { 34 //驗證ipv4地址 35 string reg = @"(?:(?:(25[0-5])|(2[0-4]\d)|((1\d{2})|([1-9]?\d)))\.){3}(?:(25[0-5])|(2[0-4]\d)|((1\d{2})|([1-9]?\d)))"; 36 string ip = ""; 37 Match m = Regex.Match(pageHtml, reg); 38 if (m.Success) 39 { 40 ip = m.Value; 41 } 42 return ip; 43 }
測試(測試時間:2019年6月18日17:37:46):編碼
1 var t0_html = HttpGetPageHtml("https://www.ip.cn", "utf-8"); 2 var t1_html = HttpGetPageHtml("http://www.ip138.com/ips138.asp", "gbk"); 3 var t2_html = HttpGetPageHtml("http://www.net.cn/static/customercare/yourip.asp", "gbk"); 4 var t0_ip = GetIPFromHtml(t0_html);// 111.198.29.123 5 var t1_ip = GetIPFromHtml(t1_html);// 111.198.29.123 6 var t2_ip = GetIPFromHtml(t2_html);// 111.198.29.123
目前這幾個url地址均可以用,但不保證長久穩定,若是想穩定可靠的使用,能夠使用一些收費的API接口,好比:http://user.ip138.com/ip/ url
————————————————————————————————————————spa