获取客户端IP地址 [英] Get Client IP Address

查看:117
本文介绍了获取客户端IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,如果客户端发送了联系详细信息,则admin需要获取客户端计算机IP地址。所以我使用以下代码来获取客户端IP地址。但是我的IP地址错误。如何在托管到实时服务器后获得客户端IP地址。

In my project,if client sent contact details, admin need to get client Computer IP address. so i used the following code to get Client IP address.But i got wrong IP address.How can i get Client IP address after hosted to live server.

string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(clientIp))
{
    string[] forwardedIps = clientIp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    clientIp = forwardedIps[forwardedIps.Length - 1];
}
else
{
    clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
Response.Write(clientIp);

推荐答案

这一行代码将为您提供你在寻找

This single line of code will give you what are you looking for
string IP= Server.HtmlEncode(Request.UserHostAddress);



当你在localhost上运行它时,它总是会给你127.0.0.1


When you run this on localhost,it will always give you 127.0.0.1


你好......

看到这个链接,可能是它的全部帮助。

http://msdn.microsoft.com/en-us/library/system .web.httprequest.userhostaddress.aspx [ ^ ]

谢谢你。
Hi...
See this link, may its help full to u.
http://msdn.microsoft.com/en-us/library/system.web.httprequest.userhostaddress.aspx[^]
Thank u.


这篇关于获取客户端IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆