【发布时间】:2018-10-09 09:56:27
【问题描述】:
在我的应用程序中,我需要获取加载应用程序的用户的 IP 地址
我尝试了各种方法来获取IP地址。但是当尝试加载应用程序时,没有获取当前IP地址。
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
string ipAddress=Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(o => o.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First().ToString()
string ipAddress=(HttpContext.Current.Request.UserHostAddress != null) ? HttpContext.Current.Request.UserHostAddress : null;
using (WebClient wc = new WebClient())
{
ipAddress = wc.DownloadString("https://api.ipify.org/");
}
【问题讨论】:
-
你在叫什么
mobile?一个手机?在什么网络上? -
“没有得到正确的 IP 地址”。你是怎么测量的?移动设备的 IP 地址不一定是公开可见的,并且可以随时更改,并且可能位于某种网络地址转换层以减少移动提供商必须提供的公共 IP 地址的数量。
-
定义“不正确”。
-
在系统或手机中,在某些情况下都无法获取正确的 IP 地址。
-
如何获取正确的IPV4地址
标签: c# ip-address