如何获得客户端或访客的IP地址::尽管多次尝试都没有解决方案:

时间:2018-11-20 06:23:53

标签: c# vb.net

如何获取客户端或访客IP地址::尽管多次尝试都没有得到解决方案:下面是我尝试过的内容:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ipAddress33 = Request.UserHostAddress.ToString();
        string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress22 = System.Net.Dns.GetHostAddresses(strHostName).GetValue(1).ToString();
        Response.Write("System.Net.Dns.GetHostAddresses(strHostName).GetValue(1).ToString(); : " + clientIPAddress22 + "<br />");
        string ipaddress;
        string IP = Request.UserHostAddress;
        string clientIPAddress = this.Page.Request.ServerVariables["REMOTE_ADDR"];
        string IP2 = Environment.GetEnvironmentVariable("CLIENTNAME");

        ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (ipaddress == "" || ipaddress == null)
            ipaddress = Request.ServerVariables["REMOTE_ADDR"];
        Response.Write("Request.ServerVariables['HTTP_X_FORWARDED_FOR'] : " + ipaddress + "<br />");
        Response.Write("Request.UserHostAddress.ToString() : " + ipAddress33 + "<br />");

        string stringIpAddress;
        stringIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (stringIpAddress == null) //may be the HTTP_X_FORWARDED_FOR is null
        {
            stringIpAddress = Request.ServerVariables["REMOTE_ADDR"];//we can use REMOTE_ADDR
        }
        Response.Write("Request.ServerVariables['REMOTE_ADDR'] : " + stringIpAddress + "<br />");

        //Get the Host Name
        string stringHostName = Dns.GetHostName();
        //Get The Ip Host Entry
        IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
        //Get The Ip Address From The Ip Host Entry Address List
        IPAddress[] arrIpAddress = ipHostEntries.AddressList;
        Response.Write("Dns.GetHostName():  " + arrIpAddress[arrIpAddress.Length - 1].ToString());

    }

}

这是我得到的结果:

System.Net.Dns.GetHostAddresses(strHostName).GetValue(1).ToString(); : 190.80.90.75
Request.ServerVariables['HTTP_X_FORWARDED_FOR'] : 190.80.90.225
Request.UserHostAddress.ToString() : 190.80.90.225
Request.ServerVariables['REMOTE_ADDR'] : 190.80.90.225
Dns.GetHostName(): 190.80.90.75

访问者的IP地址不正确。

2 个答案:

答案 0 :(得分:0)

根据this answer,您可以使用:

  protected void GetUser_IP()
{
    string VisitorsIPAddr = string.Empty;
    if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
    {
        VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
    }
    else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
    {
        VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
    }
    uip.Text = "Your IP is: " + VisitorsIPAddr;
}

答案 1 :(得分:0)

VB.Net Version::
-----------------------------------------------
Shared Sub GetUser_IP()
    If HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR") IsNot Nothing Then
        VisitorsIPAddr = HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR").ToString()
    ElseIf HttpContext.Current.Request.UserHostAddress.Length <> 0 Then
        VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress
    End If
End Sub