"Go" C # get client and server-side host information and get IP address

Source: Internet
Author: User
Tags get ip servervariables

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.