Detect/estimate country of a http-request in ASP.NET

13,275

Solution 1

You can make a simple HTTP request to this URL:

http://api.hostip.info/get_html.php?ip=207.46.197.32

using the value of the REMOTE_ADDR server variable. That will return the country and city like this:

Country: UNITED STATES (US)
City: New York, NY

I use that service for a web form just as you describe. Occasionally it doesn't know the answer, but usually it's very good (and it's free and simple 8-)

In C#, you can use System.Net.WebRequest.Create to read from the URL.

Solution 2

If you choose to use the REMOTE_ADDR server variable, you can be fairly certain that the IP that you recover accurately represents the nation of origin of that user. It is fairly uncommon for a user to be accessing the Internet from outside of the country that he is currently in, with a few notable exceptions, such as those who choose to surf though an anonymous proxy server, such as is discussed below. If, however, you want to get the state that a user is coming from, or authenticate the identity of a user, you're out of luck as far as any even remotely reliable method is concerned.

More info here.

Share:
13,275

Related videos on Youtube

Julian de Wit
Author by

Julian de Wit

Updated on April 21, 2022

Comments

  • Julian de Wit
    Julian de Wit about 2 years

    I'm looking for ways to detect/estimate the country from which a http-request is coming in ASP.NET.
    I know there are some solutions with services/country lookups but I never used one.

    I'm looking for small/clean solutions.
    It's for helping someone filling out a form so it does not have to be 100% accurate.

    Thanks in advance..

  • Transformer
    Transformer about 7 years
    hi where did you put this code? and also how can I check at login/register only