在 ASP.NET 页面中查找客户端位置 [英] Finding clients location in an ASP.NET page

查看:23
本文介绍了在 ASP.NET 页面中查找客户端位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to find clients location in my ASP.NET page? In fact I used System.Globalization.RegionInfo.CurrentRegion, but it is showing the setting in the control panel. So can I find the exact location using any method?

推荐答案

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

Not that it would give you 100% accuracy, but you can use hostip.info

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

They provide an API that gives you the location of an IP address that you pass them via HTTP request. You can use a WebClient object to make calls to the API and parse the results. Scott Hanselman has a pretty great example in this blog article (my example below is based on his article). hostip.info's database is based on an open project that the community contributes IP locations to... so there is no guarantee to be correct.

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

For starters, you need to determine the client IP address as follows:

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

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

Once you have the IP, you can create a WebClient object and call the API...

示例 API 调用:

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

结果将是如下所示的 XML:

The results will be XML that looks something like this:

<?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>

这篇关于在 ASP.NET 页面中查找客户端位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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