Finding clients location in an ASP.NET page

后端 未结 3 1709
无人及你
无人及你 2020-12-15 14:37

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 pane

3条回答
  •  时光取名叫无心
    2020-12-15 15:34

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

    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.

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

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

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

    Example API call:

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

    The results will be XML that looks something like this:

    
    
     This is the Hostip Lookup Service
     hostip
     
        inapplicable
     
     
        
         Sugar Grove, IL
         UNITED STATES
         US
         
         
            
             
                -88.4588,41.7696
             
            
         
        
     
    
    

提交回复
热议问题