如何获取客户端机器位置 [英] How to get client machine location

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

问题描述

是否可以在没有IP地址的情况下获取客户端计算机位置?我尝试了不同的代码来获取客户端机器的IP地址,但它们都没有给出正确的IP,要么给出127.0.0.1或10.0.0.3。



< b>我尝试了什么:



我尝试了以下两种方法

1)

私人字符串GetUserIP()

{

string ipList = Request.ServerVariables [HTTP_X_FORWARDED_FOR];



if(!string.IsNullOrEmpty(ipList))

{

return ipList.Split(',')[0];

}



返回Request.ServerVariables [REMOTE_ADDR];

}



这将给出127.0.0.1



2)



public static string getExternalIp()

{

试试

{

string externalIP;

externalIP =(new WebClient())。DownloadString(http://checkip.dyndns.org/);

externalIP =(新的正则表达式(@\d {1,3} \.\\\ {1,3} \。\\\ {1,3} \。\\ \\ n \\ n {1,3}))

。匹配(externalIP)[0] .ToString();

返回externalIP;

}

catch {return null; }

}



这将获得正确的IP,但需要很长时间才能获得Out put,有没有将快速获得IP的代码。

Is it possible to get client machine Location without the IP address? I tried different codes to get the client machine IP address, but none of them is giving the correct IP, either it give 127.0.0.1 Or 10.0.0.3.

What I have tried:

I tried the below two methods
1)
private string GetUserIP()
{
string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (!string.IsNullOrEmpty(ipList))
{
return ipList.Split(',')[0];
}

return Request.ServerVariables["REMOTE_ADDR"];
}

This will give 127.0.0.1

2)

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; }
}

This will get the correct IP, but it takes long time to get the Out put, Is there any code which will get the IP very fast.

推荐答案

否 - 即使使用IP地址,它也没有告诉您任何有用的信息:使用基于IP的地理定位 - 以及为什么它几乎没用。 [ ^ ]
No - and even with the IP address, it doesn't tell you anything useful: Using IP based Geolocation - and why it's pretty much useless.[^]


您已将其标记为ASP.NET,这意味着方法2将返回服务器的外部IP地址。



方法1返回环回地址,因为您正在调试Visual Studio中的代码。在该特定实例中,服务器和客户端是同一台计算机,因此 127.0.0.1 是正确的地址。



将代码部署到真实服务器后,方法1应返回客户端的正确外部IP地址。
You've tagged this as ASP.NET, which means that method 2 will return the external IP address of the server.

Method 1 is returning the loopback address because you're debugging the code in Visual Studio. In that particular instance, the server and client are the same computer, so 127.0.0.1 is the correct address.

Once you deploy your code to a real server, method 1 should return the correct external IP address of the client.


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

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