c#得到本机内网ip、外网ip
2021-04-19 02:26
标签:ade 公网 one bre response end new close eth 外网 c#得到本机内网ip、外网ip 标签:ade 公网 one bre response end new close eth 原文地址:https://www.cnblogs.com/gaocong/p/8677133.html内网
IPAddress ipAddr = Dns.Resolve(Dns.GetHostName()).AddressList[0];//获得当前IP地址
string ip = ipAddr.ToString();
//获取本机的公网IP
public static string GetPublicNetworkIP()
{
string tempip = "";
WebRequest request = WebRequest.Create("http://ip.qq.com");
request.Timeout = 10000;
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
string htmlinfo = sr.ReadToEnd();
//匹配IP的正则表达式
Regex r = new Regex("((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[1-9])", RegexOptions.None);
Match mc = r.Match(htmlinfo);
//获取匹配到的IP
tempip = mc.Groups[0].Value;
resStream.Close();
sr.Close();
return tempip;
}