获取实际的IP地址 [英] Get the actual IP address
本文介绍了获取实际的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取我的应用程序的用户IP地址.我正在使用以下代码作为
I want to get user ip address for my application. I am using following code as
txtip.Text = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
但它总是返回127.0.0.1,但我的实际IP地址是192.0.0.110.我希望使用ip.net中的编码返回此ip.
请任何人尽快帮助我.我必须完成任务.
预先感谢.
but it always return 127.0.0.1 but my actual ip address is 192.0.0.110. I want this ip to be returned using coding in asp.net .
Please any one help me soon. I have to finish the task.
Thanks in advance.
推荐答案
尝试以下操作:
Try this:
string visitorIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (String.IsNullOrEmpty(visitorIPAddress))
visitorIPAddress = Request.ServerVariables["REMOTE_ADDR"];
或
or
if (string.IsNullOrEmpty(visitorIPAddress))
visitorIPAddress = Request.UserHostAddress;
--Amit
--Amit
此代码可得出系统的确切IP地址.而不是像127.0.0.1这样的路由器地址
This code results exact ip address of system. rather than the router address like 127.0.0.1
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().UnicastAddresses[0].Address.ToString();
编码愉快.
happy coding.
这篇关于获取实际的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文