【问题标题】:Finding clients location in an ASP.NET page在 ASP.NET 页面中查找客户端位置
【发布时间】:2009-08-06 10:51:25
【问题描述】:

如何在我的 ASP.NET 页面中找到客户端位置?实际上我使用了System.Globalization.RegionInfo.CurrentRegion,但它在控制面板中显示了设置。那么我可以使用任何方法找到确切的位置吗?

【问题讨论】:

  • 使用 ASP.NET 应用程序 System.Globalization.RegionInfo.CurrentRegion 只会显示 Web 服务器的区域信息,而不是客户端 Web 浏览器。

标签: c# asp.net geolocation


【解决方案1】:

并不是说它会给你 100% 的准确率,但你可以使用 hostip.info

他们提供了一个 API,可以为您提供通过 HTTP 请求传递给他们的 IP 地址的位置。您可以使用 WebClient 对象来调用 API 并解析结果。 Scott Hanselmanthis blog article 中有一个非常棒的例子(我下面的例子是基于他的文章)。 hostip.info 的数据库基于社区提供 IP 位置的一个开放项目......所以不能保证是正确的。

对于初学者,您需要按如下方式确定客户端 IP 地址:

string ipaddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

获得 IP 后,您可以创建 WebClient 对象并调用 API...

示例 API 调用:

string r;
using (var w = new WebClient())
{
    r = w.DownloadString(String.Format("http://api.hostip.info/?ip={0}&position=true", ipaddress));
}

结果将是如下所示的 XML:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<HostipLookupResultSet version="1.0.0" xmlns="http://www.hostip.info/api" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hostip.info/api/hostip-1.0.0.xsd">
 <gml:description>This is the Hostip Lookup Service</gml:description>
 <gml:name>hostip</gml:name>
 <gml:boundedBy>
    <gml:Null>inapplicable</gml:Null>
 </gml:boundedBy>
 <gml:featureMember>
    <Hostip>
     <gml:name>Sugar Grove, IL</gml:name>
     <countryName>UNITED STATES</countryName>
     <countryAbbrev>US</countryAbbrev>
     <!-- Co-ordinates are available as lng,lat -->
     <ipLocation>
        <gml:PointProperty>
         <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
            <gml:coordinates>-88.4588,41.7696</gml:coordinates>
         </gml:Point>
        </gml:PointProperty>
     </ipLocation>
    </Hostip>
 </gml:featureMember>
</HostipLookupResultSet>

【讨论】:

    【解决方案2】:

    IPAddressExtensions 是一个免费的CodePlex 类库,如果您想要的只是 IP 地址所在的国家/地区。

    它不需要连接到另一个网站等。而且它是开源的..所以去疯了;)

    【讨论】:

      【解决方案3】:

      您可以通过查找客户端 IP 地址来猜测 IP 地址所属的国家/地区。有些页面提供了包含 IP 到国家/地区映射表的数据库。例如,请参阅 here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-10
        相关资源
        最近更新 更多