Tutorialsteacher

Follow Us

Query geolocation & proxy data in .NET using IP2Location library

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:myfolderIP2LOCATION-LITE-DB11.BIN";
            oIPResult = oIP2Location.IPQuery(strIPAddress);
        
            switch (oIPResult.Status.ToString())
            {
                case "OK":
                    result += "IP2Location GeoLocation Results:
===========================================
";
                    result += "IP Address: " + oIPResult.IPAddress + "
";
                    result += "Country Code: " + oIPResult.CountryShort + "
";
                    result += "Country Name: " + oIPResult.CountryLong + "
";
                    result += "Region: " + oIPResult.Region + "
";
                    result += "City: " + oIPResult.City + "
";
                    result += "Latitude: " + oIPResult.Latitude + "
";
                    result += "Longitude: " + oIPResult.Longitude + "
";
                    result += "Postal Code: " + oIPResult.ZipCode + "
";
                    result += "ISP Name: " + oIPResult.InternetServiceProvider + "
";
                    result += "Domain Name: " + oIPResult.DomainName + "
";
                    result += "Time Zone: " + oIPResult.TimeZone + "
";
                    result += "Net Speed: " + oIPResult.NetSpeed + "
";
                    result += "IDD Code: " + oIPResult.IDDCode + "
";
                    result += "Area Code: " + oIPResult.AreaCode + "
";
                    result += "Weather Station Code: " + oIPResult.WeatherStationCode + "
";
                    result += "Weather Station Name: " + oIPResult.WeatherStationName + "
";
                    result += "MCC: " + oIPResult.MCC + "
";
                    result += "MNC: " + oIPResult.MNC + "
";
                    result += "Mobile Brand: " + oIPResult.MobileBrand + "
";
                    result += "Elevation: " + oIPResult.Elevation + "
";
                    result += "Usage Type: " + oIPResult.UsageType + "
";
                    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:myfolderIP2PROXY-LITE-PX8.BIN", IP2Proxy.Component.IOModes.IP2PROXY_FILE_IO) == 0)
    {
        all = proxy.GetAll(strIPAddress);
        result += "

IP2Proxy Proxy Results:
===========================================
";
        result += "Is_Proxy: " + all.Is_Proxy.ToString() + "
";
        result += "Proxy_Type: " + all.Proxy_Type + "
";
        result += "Country_Short: " + all.Country_Short + "
";
        result += "Country_Long: " + all.Country_Long + "
";
        result += "Region: " + all.Region + "
";
        result += "City: " + all.City + "
";
        result += "ISP: " + all.ISP + "
";
        result += "Domain: " + all.Domain + "
";
        result += "Usage_Type: " + all.Usage_Type + "
";
        result += "ASN: " + all.ASN + "
";
        result += "AS: " + all.AS + "
";
        result += "Last_Seen: " + all.Last_Seen + "
";
        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.