如何在C#中获取客户端REAL IP地址? [英] How can I get the client REAL IP address in C# ?
本文介绍了如何在C#中获取客户端REAL IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我!
如何在ASP.NET页面中获取客户端的真实IP地址?
我尝试了很多方法来尝试获得这个值,但是失败了。
这是我尝试过的方法:
Request.UserHostAddress
Request.ServerVariables [ HTTP_X_FORWARDED_FOR]
Request.ServerVariables [ REMOTE_ADDR]
结果是:
UserHostAddress: 16 . 85 。 175 。 133
HTTP_X_FORWARDED_FOR:
REMOTE_ADDR: 16 。 85 。 175 。 133
这个它不是客户端IP地址的代理地址。
获得此价值的任何解决方案?
谢谢!
解决方案
你在外面看到的IP地址是什么意思。
要获得此IP地址,您必须使用手动或以编程方式(通过API)提供此信息的服务。
例如,要使用''http://whatismyip.com'',以下代码将以编程方式获取IP:
UTF8Encoding utf8 = < span class =code-keyword> new UTF8Encoding();
WebClient mywebClient = new WebClient();
String externalIp = utf8.GetString(mywebClient.DownloadData( http://whatismyip.com/automation/n09230945.asp跨度>));
Response.Write( 您的外部IP地址为: + externalIp );
Please help me!
How can I get the client''s real IP address in ASP.NET page?
I have tried many ways to try to get this value, but failed.
Here is my tried methods:
Request.UserHostAddress
Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
Request.ServerVariables["REMOTE_ADDR"]
The result is:
UserHostAddress: 16.85.175.133
HTTP_X_FORWARDED_FOR:
REMOTE_ADDR: 16.85.175.133
This is not client IP address, it''s Proxy address.
Any solutions to get this value ?
Thank you!
解决方案
What you mean in the IP address that is seen from outside.
To get this IP address, you must use a service that provide this information manually or programatically (via API).
For example, to use ''http://whatismyip.com'' the following code will obtain the IP programatically:
UTF8Encoding utf8 = new UTF8Encoding(); WebClient mywebClient = new WebClient(); String externalIp = utf8.GetString(mywebClient.DownloadData("http://whatismyip.com/automation/n09230945.asp")); Response.Write("Your External IP Address is: " + externalIp );
这篇关于如何在C#中获取客户端REAL IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文