ASP.NET:获取外部IP地址
问题描述:
我开发的网站。我需要让网站访问者的IP。
我尝试使用请求,但只有内部IP:
I developed site. I need to get IP of site visitors. I try to use Request, but it have only internal IP:
Response.Write(Request.ServerVariables["REMOTE_ADDR"]);
我看着在服务器变量集合中的所有键 - 相同的结果:
I looked all keys in Server Variables collection - the same result:
foreach (string var in Request.ServerVariables)
{
Response.Write(Request[var]);
}
我怎样才能获得外部IP地址?
How can I get external IP address?
答
您可以用它来获得外部(公共)IP地址。
You can use it to get External (public) IP Address..
public static string getExternalIp()
{
try
{
string externalIP;
externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
.Matches(externalIP)[0].ToString();
return externalIP;
}
catch { return null; }
}