【问题标题】:I have fetch the ip address of my website visitor, how can i get their geographic location too?我已经获取了我的网站访问者的 IP 地址,我怎样才能得到他们的地理位置呢?
【发布时间】:2012-06-09 06:25:55
【问题描述】:

我想获取我的网站访问者的地理位置并将其存储在我的数据库中,已经获取了 IP 地址。

我正在使用 asp.net 3.0

protected void Page_Load( object sender, EventArgs e)
    {
        // Track Visitors
        string ipAddress = IpAddress();
        string hostName = Dns .GetHostByAddres(ipAddress).HostName;
        StreamWriter wrtr = new StreamWriter (Server.MapPath( "visitors.log" ), true );
        wrtr.WriteLine( DateTime .Now.ToString() + " | " + ipAddress + " | " + hostName + " | " + Request.Url.ToString());
        wrtr.Close();
    }

    private string IpAddress()
    {
        string strIpAddress;
        strIpAddress = Request.ServerVariables[ "HTTP_X_FORWARDED_FOR" ];
        if (strIpAddress == null )
            strIpAddress = Request.ServerVariables[ "REMOTE_ADDR" ];
        return strIpAddress;
    }

【问题讨论】:

  • 这只是给了我IP地址n主机名,我也想知道城市和国家,,,,

标签: asp.net geolocation ip-address


【解决方案1】:

没有内置的方法。互联网上有提供此类映射的网络服务。此类数据库“GeoIP 数据库”也可供本地使用。

但最好订阅更新的网络服务并使用它来获取位置。 Google's Client Location API's 应该可以帮到你。

一些现代浏览器会向您发送用户位置。你可以用那个。但由于并非所有浏览器都遵循这一点,因此并不可靠。您可以查看 w3.org 提供的Geo Location API

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2013-02-14
    • 1970-01-01
    • 2015-08-06
    • 2022-12-19
    相关资源
    最近更新 更多