hi all,

how to Get country, state, zip code and Latitude and Longitude from an ip address?

Thanks,
kk

Dani AI

Generated

A short, practical add-on to what was posted above: the usual, reliable workflow is (1) capture the visitor IP on the server, (2) sanitize/choose the correct IP from proxy headers, then (3) resolve that IP with a geolocation provider or a WHOIS/RDAP lookup for the network owner (ISP). The ASP.NET approach shown by is a common starting point, but forwarded headers can be spoofed and must only be trusted when they come from known/trusted proxies. (rfc-editor.org)

For the geolocation step there are two typical options: a local database (MaxMind GeoIP2 / GeoLite2) or a web API (ip-api, IPinfo, ipstack, etc.). Local DBs are fast and avoid rate limits; web APIs are easier to integrate and can return ISP/ASN, postal code, latitude/longitude, city and state fields directly. Example quick test using the free ip-api JSON endpoint:

curl "http://ip-api.com/json/8.8.8.8?fields=status,country,regionName,city,zip,lat,lon,isp,query"

(ip-api shows which fields it returns in its docs). Accuracy varies by provider and by granularity — country-level is very reliable; city/postal-level is often approximate. See MaxMind’s discussion of accuracy. (ip-api.com)

For the ISP/organization tied to an IP, either use a dedicated ISP/ASN dataset (MaxMind’s GeoIP2 ISP database) or an authoritative RDAP/WHOIS lookup from the relevant RIR (ARIN, RIPE, APNIC). RDAP returns the registered network holder and contact metadata. Use an ISP database for bulk or realtime classification; use RDAP/WHOIS for authoritative reference or auditing. (dev.maxmind.com)

Practical tips: accept X-Forwarded-For but parse the comma list and pick the first non-private IP; check provider-specific headers when behind a CDN (Cloudflare provides CF-Connecting-IP); treat postal code/lat-long as approximate and update local DBs frequently; add graceful fallbacks if fields are blank (many providers blank detailed fields for some networks). (developers.cloudflare.com)

References:

Recommended Answers

All 5 Replies

Yes, and why not ask for the type of dog if there is any on this ip adress?
Why not read this?

hey ddanbe,
what is this?
thanks.

finally i got solution.

but how can i get ISP ip address?

thanks.

To get the IP address of a visitor (ASP.NET):

protected string GetIPAddress()
{
    System.Web.HttpContext context = System.Web.HttpContext.Current;
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
    }

    return context.Request.ServerVariables["REMOTE_ADDR"];
}

To get local IP address:

public string LocalIPAddress()
{
    IPHostEntry host;
    string localIP = "";
    host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (IPAddress ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            localIP = ip.ToString();
            break;
        }
    }
    return localIP;
}

thanks Anthony21 for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.