Back

Query geolocation & proxy data in .NET using IP2Location


In this tutorial, we will show how to input an IP address and get back IP geolocation & proxy data using IP2Location library. Developers can use the geolocation information to deliver business requirements such are pages redirection and fraud prevention.

Pre-requisites

  • Microsoft Visual Studio to compile the codes.
  • Microsoft .NET Framework 4.61 or later.
  • IP2Location LITE DB11 BIN database.
  • IP2Proxy LITE PX8 BIN database.

We will assume you already have a version of Microsoft Visual Studio that is capable of handling .NET Framework 4.61 or later.

Getting started

First of all, you need to download the free IP geolocation BIN databases for both the IP2Location and the IP2Proxy data. Both databases are free for use with attribution required.

Download the free IP2Location LITE DB11 data:

https://lite.ip2location.com/ip2location-lite

Download the free IP2Proxy LITE PX8 data:

https://lite.ip2location.com/ip2proxy-lite

After downloading both zipped files above, you need to extract out their respective BIN files and copy them to a folder somewhere, e.g. C:\myfolder\ for our example.

In your Visual Studio project, go to the NuGet Package Manager and install the 2 NuGet packages below:

https://www.nuget.org/packages/IP2Location.IPGeolocation/

https://www.nuget.org/packages/IP2Proxy/

How to query both components

We will just show how to create and call the components.

First, create the QueryIP2Location function below will accept an IP address and output the geolocation results.

Example: Get Geolocation from IP Address
private string QueryIP2Location(string strIPAddress)
{
    IP2Location.IPResult oIPResult = new IP2Location.IPResult();
    IP2Location.Component oIP2Location = new IP2Location.Component();
    String result = String.Empty;
    
    try
    {
        if (strIPAddress != "")
        {
            oIP2Location.IPDatabasePath = @"C:\myfolder\IP2LOCATION-LITE-DB11.BIN";
            oIPResult = oIP2Location.IPQuery(strIPAddress);
        
            switch (oIPResult.Status.ToString())
            {
                case "OK":
                    result += "IP2Location GeoLocation Results:\n===========================================\n";
                    result += "IP Address: " + oIPResult.IPAddress + "\n";
                    result += "Country Code: " + oIPResult.CountryShort + "\n";
                    result += "Country Name: " + oIPResult.CountryLong + "\n";
                    result += "Region: " + oIPResult.Region + "\n";
                    result += "City: " + oIPResult.City + "\n";
                    result += "Latitude: " + oIPResult.Latitude + "\n";
                    result += "Longitude: " + oIPResult.Longitude + "\n";
                    result += "Postal Code: " + oIPResult.ZipCode + "\n";
                    result += "ISP Name: " + oIPResult.InternetServiceProvider + "\n";
                    result += "Domain Name: " + oIPResult.DomainName + "\n";
                    result += "Time Zone: " + oIPResult.TimeZone + "\n";
                    result += "Net Speed: " + oIPResult.NetSpeed + "\n";
                    result += "IDD Code: " + oIPResult.IDDCode + "\n";
                    result += "Area Code: " + oIPResult.AreaCode + "\n";
                    result += "Weather Station Code: " + oIPResult.WeatherStationCode + "\n";
                    result += "Weather Station Name: " + oIPResult.WeatherStationName + "\n";
                    result += "MCC: " + oIPResult.MCC + "\n";
                    result += "MNC: " + oIPResult.MNC + "\n";
                    result += "Mobile Brand: " + oIPResult.MobileBrand + "\n";
                    result += "Elevation: " + oIPResult.Elevation + "\n";
                    result += "Usage Type: " + oIPResult.UsageType + "\n";
                    break;
                case "EMPTY_IP_ADDRESS":
                    result += "IP Address cannot be blank.";
                    break;
                case "INVALID_IP_ADDRESS":
                    result += "Invalid IP Address.";
                    break;
                case "MISSING_FILE":
                    result += "Invalid Database Path.";
                    break;
            }
        }
        else
        {
            result += "IP Address cannot be blank.";
        }
    }
    catch (Exception ex)
    {
        result += ex.Message;
    }
    finally
    {
        oIPResult = null;
        oIP2Location = null;
    }
    
    return result;
}

Next, we will create the following QueryIP2Proxy function which also takes an IP address and output the proxy information.

Example: Get Proxy info from IP Address
private void QueryIP2Proxy(string strIPAddress)
{
    IP2Proxy.Component proxy = new IP2Proxy.Component();
    IP2Proxy.ProxyResult all;
    String result = String.Empty;

    if(proxy.Open(@"C:\myfolder\IP2PROXY-LITE-PX8.BIN", IP2Proxy.Component.IOModes.IP2PROXY_FILE_IO) == 0)
    {
        all = proxy.GetAll(strIPAddress);
        result += "\n\nIP2Proxy Proxy Results:\n===========================================\n";
        result += "Is_Proxy: " + all.Is_Proxy.ToString() + "\n";
        result += "Proxy_Type: " + all.Proxy_Type + "\n";
        result += "Country_Short: " + all.Country_Short + "\n";
        result += "Country_Long: " + all.Country_Long + "\n";
        result += "Region: " + all.Region + "\n";
        result += "City: " + all.City + "\n";
        result += "ISP: " + all.ISP + "\n";
        result += "Domain: " + all.Domain + "\n";
        result += "Usage_Type: " + all.Usage_Type + "\n";
        result += "ASN: " + all.ASN + "\n";
        result += "AS: " + all.AS + "\n";
        result += "Last_Seen: " + all.Last_Seen + "\n";
        proxy.Close();
    }
    else
    {
        result += "Error reading BIN file.";
    }
    
    return result;
}

Finally, we just call both functions to get the information we need.

QueryIP2Location("8.8.8.8");
QueryIP2Proxy("8.8.8.8");

It is just that easy to implement IP geolocation and proxy detection functionalities in your code.

Visit blog.ip2location.com to know more about it.