简体   繁体   中英

Get IP-address of HTTP client

I try to get IP-address and port of client which sends HTTP requests to the server. I used properties of HttpListenerRequest class. UserHostAddress property returns ip and port of server but not client. RemoteEndPoint returns some ip and port but it is not true client ip (I know that client requests are coming from port 1234) and every time it returns different data (I don't know why). How can I solve this? May be I should set http header From on the client side?

Thank you very much!

based on SO answer, I think this would help you...

   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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM