How to get visitor location ( country, state and c

2019-01-16 09:51发布

问题:

I want to implement this in ASP.NET. I don't have any idea about how to do so, unfortunately.

回答1:

Here how it is done in asp.net

Request.ServerVariables("REMOTE_ADDR")

Get a copy of IP adress database by location here

http://www.maxmind.com/



回答2:

Why not use Google Analytics? You will get more than what you need. Alternatively you can get the client's ip and use service like ip2location to get the location.

Check this similar question as well. finding clients location in asp.net page.



回答3:

There are a few free web services out there which provide IP-to-location services. For instance:

http://freegeoip.net/
http://www.hostip.info/
http://ipinfodb.com/ip_location_api.php



回答4:

by using

string userHost = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (String.IsNullOrEmpty(userHost) || 
    String.Compare(userHost, "unknown", true) == 0)
{
    userHost = Request.UserHostAddress;
}

you will get users ip address . Based on this ip address you can find out visitor location details by calling some webservice .



回答5:

IPAddressExtensions is a free codeplex class library if all you just want is the Country the IP is located from.



回答6:

First, get the IP address of the visitor using Request.ServerVariables("REMOTE_ADDR"). Bear in mind that the visitor could be using a proxy server in which case the IP address may not be their actual IP address. For the proxy case, you can check if Request.ServerVariables("HTTP_X_FORWARDED_FOR") contains a value. This will be the actual IP address if the proxy server is not an anonymous proxy server.

Then you have 2 options, using a web service or querying data from your own database. Either way, you will need data which can match an IP address of the visitor to their country, state and city.



回答7:

Get the client IP and find the location of IP using any IP to geo location Mapping service.



回答8:

This is what I've used:

<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            'url': 'http://www.freegeoip.net/json/@(HttpContext.Current.Request.UserHostAddress)',
            'type': 'GET',
            'success': function(data) {
                // for example
                if (data.country_code === "GB") {
                    ... further logic here
                }
            }
        });
    });
</script>