The original Published time: 2009-10-28--from my Baidu article [imported by moving tools]
Summary:
1, REMOTE_ADDR can not be modified, but may obtain the proxy server IP, not the actual client IP.
2, through Http_via, http_x_forwarded_for we can get Proxy server proxy information, but this relies on proxy server support. In addition, these two values can be modified. The information we get through it may not be true. Also, the http_x_forwarded_for information may be a collection that does not contain proxy IP in the REMOTE_ADDR
3.//Get the client IP address
public static string GetIP ()
{
string StrIP = String.Empty;
StrIP = httpcontext.current.request.servervariables["Http_x_forwarded_for"];
if (string. IsNullOrEmpty (StrIP))
{
StrIP = httpcontext.current.request.servervariables["REMOTE_ADDR"];
}
if (string. IsNullOrEmpty (StrIP))
{
StrIP = HttpContext.Current.Request.UserHostAddress;
}
if (string. IsNullOrEmpty (StrIP))
{
return "0.0.0.0";
}
return StrIP;
}
Client IP:
Request.ServerVariables.Get ("REMOTE_ADDR"). ToString ();
Client Host Name:
Request.ServerVariables.Get ("Remote_host"). ToString ();
client browser ie:
Request.Browser.Browser;
client browser version number:
request.browser.majorversion;//
Client operating system:
Request.Browser.Platform;
Server IP:
Request.ServerVariables.Get ("Local_addr"). ToString ();
Server name:
Request.ServerVariables.Get ("SERVER_NAME"). ToString ();
If you want to learn more about ServerVariables, you can use
foreach (String o in request.servervariables) {
Response.Write (o+ "=" +request.servervariables[o]+ "<br>");
}
For those who understand the proxy server situation, we will know that if the user uses a proxy server, the above code obtains the IP address of the proxy server, if the user uses more than one proxy server, it is the IP address of the last proxy server to reach the server.
REMOTE_ADDR Description:
The IP address of the access client.
This information cannot be modified by the user.
If you really change, you can not connect with the server, the server is to follow this to establish a connection with the client and communicate. Actually I test to modify this ServerVariables, no effect at all. Still getting is the actual value.
Also: Request.userhostaddress and request.servervariables["REMOTE_ADDR" are actually the same value.
"Go" C # get client and server-side host information and get IP address