开发者

How to get the IP address from a visitor (also if behind a Proxy) [duplicate]

This q开发者_如何转开发uestion already has answers here: Closed 11 years ago.

Possible Duplicate:

Client IP using C#

I use ASP.net and C# and I would like to know how to get the IP address from a visitor on a page.

I would like to see an example of code that retrieves the IP address and also will be able to show if an IP was behind a proxy.

Thanks for your time.


You could use the UserHostName property on the Request object:

string ip = Request.UserHostName;

As far as your second question about the proxy is concerned, there is no reliable way to achieve this. You could use heuristics to look for some HTTP request headers that might be sent by the proxy server such as Via or X-Forwarded-For.

string header = Request.Headers["Via"] ?? Request.Headers["X-Forwarded-For"];
if (!string.IsNullOrEmpty(header))
{
    // probably the request was forwarded from a proxy server
    // but you cannot be 100% sure as HTTP request headers can be faked
}


so simple dude,

string IP = Request.ServerVariables["REMOTE_HOST"].ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜