How would i determine the country that a spcific IP address is originating from using c#. I need to use this to check if connections originate from a specific country.
You can use this SQL data in your project to determine that: IP address geolocation SQL database. Download that data and import it into your database to run checks locally.
Or you can use their free API that returns XML containing the country code and country name. You'd make a request to the following URL with the IP address you wanted to check, as seen in this example:
http://ipinfodb.com/ip_query_country.php?ip=74.125.45.100
Returns:
<Response>
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
</Response>
If you don't want to use an API like perhaps hostip.info, then I'd suggest subscribing to maxmind and running a host lookup database locally.
you can ask google to do it for you.
there are also services that you can pay for you want:
ip2cc - Lookup country and Russia region by IP address Python module with script to create database from up-to-date official data.
This Python
utility loads (as frequently as you like) up-to-date information from Regional Internet Registry sites (arin
, ripencc
, apnic
, lacnic
, afrinic
), as shown in the source:
url_template = 'ftp://ftp.ripe.net/pub/stats/%s/delegated-%s-latest'
sources = {}
for name in ('arin', 'ripencc', 'apnic', 'lacnic', 'afrinic'):
sources[name] = url_template % (name, name)
Once the data is loaded, queries can be answered offline and very quickly.
Can be easily modified to directly answer the original question, or used from the command line to return the country an IP address
belongs to.
If only you could do it online because there are free APIs and web services out there you can query for the latest data. Also you won't have to worry about outdated data as generally speaking these services do tend to keep their data up-to-date.
Since you wanted an offline option, your best option would be a paid service which allow you to download the latest data every month like IP2Location.