[英]ASP.NET question regarding getting the IP address of a user
我正在尝试使用以下方法获取IP地址:
protected void Page_Load(object sender, EventArgs e)
{
string ClientIP;
ClientIP = HttpContext.Current.Request.UserHostAddress;
Label1.Text = ClientIP;
}
此代码输出127.0.0.1。
下面的代码什么也不显示!
string ClientIP;
ClientIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Label1.Text = ClientIP;
我该如何获取IP地址? 用户的真实IP地址?
[编辑]
我不想确切的位置顺便说一句。 我需要知道国家/地区,然后将用户重定向到相应的网页。
Request.Params["REMOTE_ADDR"]
使用System.Net,请尝试以下操作-
// Then using host name, get the IP address list..
IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.