我正在尝试使用以下方式获取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归因
不隶属于 StackOverflow
scroll top