在ASP.NET页面查找客户所在地 [英] Finding clients location in an ASP.NET page

查看:76
本文介绍了在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

他们提供给你,你通过HTTP请求传递它们的IP地址的位置的API。您可以使用WebClient的对象做出的API调用和分析结果。 斯科特Hanselman的在<一个一个pretty很好的例子href=\"http://www.hanselman.com/blog/TheWeeklySource$c$c37GeolocationGeotargetingReverseIPAddressLookupInASPNETMVCMadeEasy.aspx\"相对=nofollow>这个博客文章(我下面的例子是基于他的文章)。 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天全站免登陆