1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > C#获取外网IP

C#获取外网IP

时间:2023-10-29 14:07:13

相关推荐

C#获取外网IP

public static string getExternalIp(){try{WebClient client = new WebClient();client.Encoding = System.Text.Encoding.Default;//string response = client.DownloadString("/ic.asp");//失效了//string response = client.DownloadString("/");//可用,可能不稳定string response = client.DownloadString("/");//站长之家string myReg = @"<dd class=""fz24"">([\s\S]+?)<\/dd>";Match mc = Regex.Match(response, myReg, RegexOptions.Singleline);if (mc.Success && mc.Groups.Count > 1){response = mc.Groups[1].Value;return response;}else{return "Can't get you Ip address!";}}catch (Exception){return "Can't get you Ip address!";}}//获取宽带连接(PPPOE拨号)的IP地址,timeout超时(秒),当宽带未连接或者连接中的时候获取不到IPpublic static string GetIP_PPPOE(int timeout){int i = timeout * 2;while (i > 0){try{NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();bool havePPPOE = false;foreach (NetworkInterface adapter in nics){if (workInterfaceType == NetworkInterfaceType.Ppp){havePPPOE = true;IPInterfaceProperties ip = adapter.GetIPProperties();//IP配置信息if (ip.UnicastAddresses.Count > 0){return ip.UnicastAddresses[0].Address.ToString();}}}//当没有宽带连接的时候直接返回空if (!havePPPOE) return "";}catch (Exception ex){Console.WriteLine("获取宽带拨号IP出错:" + ex.Message);}i--;Thread.Sleep(500);}return "";}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。