How to determine if an IP address belongs to a country

前端 未结 11 1142
情歌与酒
情歌与酒 2020-12-07 17:55

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.

相关标签:
11条回答
  • 2020-12-07 18:40

    You can easily do this using an IP Geolocation API

    It will return the country, state, city, timezone, and postal code. Also, visit https://geo.ipify.org/docs and try for free. Let me know how it goes.

    0 讨论(0)
  • 2020-12-07 18:48

    you can ask google to do it for you.

    • http://googlecode.blogspot.com/2008/08/two-new-ways-to-location-enable-your.html

    there are also services that you can pay for you want:

    • http://www.ip2location.com/
    • http://www.maxmind.com/
    0 讨论(0)
  • 2020-12-07 18:48

    Here is a free IP Address to Country database.

    0 讨论(0)
  • 2020-12-07 18:48

    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.

    0 讨论(0)
  • 2020-12-07 18:49

    You can easily lookup any IP address using https://astroip.co

    In C#:

    var url = "https://api.astroip.co/70.167.7.1/?api_key=1725e47c-1486-4369-aaff-463cc9764026";
    
    using (var httpClient = new HttpClient())
    using(var resp = await httpClient.GetAsync(url))
    {
        string resp = await resp.Content.ReadAsStringAsync();
     }
    

    In curl:

    curl https://api.astroip.co/70.167.7.1/?api_key=1725e47c-1486-4369-aaff-463cc9764026

    {
      "status_code": 200,
      "geo": {
        "is_metric": false,
        "is_eu": false,
        "longitude": -97.822,
        "latitude": 37.751,
        "country_geo_id": 6252001,
        "zip_code": null,
        "city": null,
        "region_code": null,
        "region_name": null,
        "continent_code": "NA",
        "continent_name": "North America",
        "capital": "Washington",
        "country_name": "United States",
        "country_code": "US"
      },
      "asn": {
        "route": "70.167.6.0/23",
        "type": "isp",
        "domain": "cox.net",
        "organization": "ASN-CXA-ALL-CCI-22773-RDC",
        "asn": "AS22773"
      },
      "currency": {
        "native_name": "US Dollar",
        "code": "USD",
        "name": "US Dollar",
        "symbol": "$"
      },
      "timezone": {
        "is_dst": false,
        "gmt_offset": -21600,
        "date_time": "2020-11-23T15:15:56-06:00",
        "microsoft_name": "Central Standard Time",
        "iana_name": "America/Chicago"
      },
      "security": {
        "is_crawler": false,
        "is_proxy": false,
        "is_tor": false,
        "tor_insights": null,
        "proxy_insights": null,
        "crawler_insights": null
      },
      "crypto": null,
      "user_agent": null,
      "error": null,
      "hostname": "wsip-70-167-7-1.sd.sd.cox.net",
      "ip_type": "ipv4",
      "ip": "70.167.7.1"
    }
    
    0 讨论(0)
提交回复
热议问题