C#获取本地IP地址,内网+外网方法

2021-03-10 10:28

阅读:495

标签:isp   efault   多个   default   转换   for   主机   new   contain   

 1 #region 获取内、外网Ip
 2 
 3         /// 
 4         /// 获取本地ip地址,优先取内网ip
 5         /// 
 6         public static String GetLocalIp()
 7         {
 8             String[] Ips = GetLocalIpAddress();
 9 
10             foreach (String ip in Ips) if (ip.StartsWith("10.80.")) return ip;
11             foreach (String ip in Ips) if (ip.Contains(".")) return ip;
12 
13             return "127.0.0.1";
14         }
15 
16         /// 
17         /// 获取本地ip地址。多个ip
18         /// 
19         public static String[] GetLocalIpAddress()
20         {
21             string hostName = Dns.GetHostName();                    //获取主机名称  
22             IPAddress[] addresses = Dns.GetHostAddresses(hostName); //解析主机IP地址  
23 
24             string[] IP = new string[addresses.Length];             //转换为字符串形式  
25             for (int i = 0; i  addresses[i].ToString();
26 
27             return IP;
28         }
29 
30         /// 
31         /// 获取外网ip地址
32         /// 
33         public static string GetExtenalIpAddress_0()
34         {
35             string IP = "未获取到外网ip";
36             try
37             {
38                 //从网址中获取本机ip数据  
39                 System.Net.WebClient client = new System.Net.WebClient();
40                 client.Encoding = System.Text.Encoding.Default;
41                 string str = client.DownloadString("http://1111.ip138.com/ic.asp");
42                 client.Dispose();
43 
44                 //提取外网ip数据 [218.104.71.178]  
45                 int i1 = str.IndexOf("["), i2 = str.IndexOf("]");
46                 IP = str.Substring(i1 + 1, i2 - 1 - i1);
47             }
48             catch (Exception) { }
49 
50             return IP;
51         }
52 
53         /// 
54         /// 获取外网ip地址
55         /// 
56         public static string GetExtenalIpAddress()
57         {
58             String url = "http://hijoyusers.joymeng.com:8100/test/getNameByOtherIp";
59             string IP = "未获取到外网ip";
60             try
61             {
62                 //从网址中获取本机ip数据  
63                 System.Net.WebClient client = new System.Net.WebClient();
64                 client.Encoding = System.Text.Encoding.Default;
65                 string str = client.DownloadString(url);
66                 client.Dispose();
67 
68                 if (!str.Equals("")) IP = str;
69                 else IP = GetExtenalIpAddress_0();
70             }
71             catch (Exception) { }
72 
73             return IP;
74         }
75 
76         #endregion

为了获取IP 我寻找了很多方法 大部分还是获得全部IP

//获取全部IP  
string name = Dns.GetHostName(); IPAddress[] ipadrlist = Dns.GetHostAddresses(name);


//获取全部IP IPHostEntry myEntry
= Dns.GetHostEntry(Dns.GetHostName()); myEntry.AddressList.FirstOrDefault(e => e.AddressFamily.ToString().Equals("InterNetwork")).ToString(); /// /// 获取本地IP地址信息 /// void GetAddressIP() { ///获取本地的IP地址 string AddressIP = string.Empty; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { AddressIP = _IPAddress.ToString(); } } string Text = AddressIP; }

 

C#获取本地IP地址,内网+外网方法

标签:isp   efault   多个   default   转换   for   主机   new   contain   

原文地址:https://www.cnblogs.com/ning-xiaowo/p/12696959.html


评论


亲,登录后才可以留言!