.Net如何获取IP地址 .Net获取IP地址的方法

获取本机IP

 

获取网页客户端IP

 

获取网页IP

public static string GetIP4Address(HttpRequest request = null)    
  {      
  Func f = e => e.AddressFamily.ToString() == "InterNetwork";      
  IPAddress ipa = Dns.GetHostAddresses((request ?? HttpContext.Current.Request).UserHostAddress).FirstOrDefault(f)        
  ?? Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(f);      
  if (ipa != null)     
  {       
  return ipa.ToString();    
  }      
  return HttpContext.Current.Request.UserHostAddress;   
  }

 

posted @ 2022-09-16 11:14  Rukh  阅读(727)  评论(0编辑  收藏  举报