如何获取客户端计算机的客户端IPv4地址。 [英] How to get the client IPv4 address of the client machine.
本文介绍了如何获取客户端计算机的客户端IPv4地址。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取客户端计算机的IP地址。
i已尝试使用某些代码,但所有时间我都在获取服务器的IP地址。 />
如何获取客户端计算机的IP地址?
How to get the Ip address of the client machine.
i have tried with some code but all time I am getting the Ip address of the server.
How do I get the Ip address of the client machines?
string ip = "";
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
ip = addr[addr.Length - 1].ToString();
提前致谢
Thanks in advance
推荐答案
示例
Example
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;
}
试试这个,
Try this,
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
这篇关于如何获取客户端计算机的客户端IPv4地址。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文