Using VB.NET To Check for Proxy and VPN With IP2Location.io Geolocation API
Many websites use IP addresses to track users. This tutorial explains how to obtain geolocation data and proxy information with VB.NET.
Join the DZone community and get the full member experience.
Join For FreeVirtual Private Network (VPN) servers are proxy servers that people use daily when browsing the Internet. They use it because it shields them from being tracked by websites. As most of us are aware, websites track their visitors for advertising and marketing purposes. That’s how they can serve ads across multiple websites. By using a VPN when browsing, that no longer happens.
That’s the same reason that people use residential proxy servers. As residential proxy servers are actually home computers that are used by real individuals, websites have a much harder time differentiating between a real user and a proxy user.
Why Is Proxy Server Detection Vital for Websites?
Imagine you’re an online merchant, and you need to screen online order transactions for fraud. It’s common to match the user’s IP geolocation country against the billing or shipping address. If there is no match, it’s very likely to be a fraudster trying to use stolen credit cards to buy things. A proxy server can make the user appear to originate from a different country than their actual location. Being able to detect if the user is on a VPN or a residential proxy gives the merchant the option to block or request identity verification. Doing so can severely curtail potential fraudulent orders from ruining the bottom line of the company.
Return the Geolocation and Proxy Details for an IP Address
Website developers can utilize the IP2Location.io .NET SDK to query the IP2Location.io API for their website visitor’s IP address. Just pump in the IP address and get back IP geolocation data as well as proxy info in the same result.
Armed with this data, websites can offer customized content by region. Ads can also be tailored to visitors coming from specific areas to optimize the marketing ROI. Global websites can redirect visitors from certain countries to country-specific pages. This helps with sales conversion as country-specific pages can feature the local language, display prices in the native currency, and offer a more familiar touch to customers.
Meanwhile, the proxy data returned by the API can indicate if the user is utilizing a VPN or residential proxy servers. Online sellers need to detect such proxy usages as malicious actors frequently use them to commit order fraud. By hiding behind a proxy server, the website is unable to determine the actual geolocation for the user. Order fraud screening relies on accurate geolocation to cross check the IP geolocation country vs. the billing or shipping address. With a proxy, a scammer in Nigeria can pretend to be a shopper located in the United States.
Let’s See How To Use VB.NET to Query IP2Location.io for Proxy and Geolocation
Take note that we’re going to be using Visual Studio 2022 to create our code and run it. But before we proceed to the coding side, get the IP2Location.io API key if you haven’t done so.
If you’re only interested in basic geolocation, the free plan will suffice. Else, you may need to subscribe to a higher plan.
Create a New VB.NET Project
Launch Visual Studio, then click on Create a new project. Select Console App and click Next.
We’ll call our project IP2LocationIODemo as below and click Next.
For the framework, we’ll go with .NET 7.0, then click Create.
Now, you should see the editor below.
Install Prerequisites
Since our code relies on the IP2Location.io .NET SDK, let’s install that. We also need to install the Newtonsoft.Json package.
In the Visual Studio menu, click on Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution, then install the Newtonsoft.Json package.
Do the same for the IP2Location.io package. You may need to search “IP2Location.io” to find it.
Add in the VB.NET Sample Code
Let’s edit the Program.vb code and replace it with the below code. Remember to edit the code to use your own IP2Location.io API key.
Imports Newtonsoft.Json
Imports IP2LocationIOComponent
Module Program
Sub Main()
' Configures IP2Location.io API key
Dim Config = New Configuration With {
.ApiKey = "YOUR_API_KEY"
}
Dim IPL As New IPGeolocation(Config)
Dim IP As String = "8.8.8.8"
Dim Lang As String = "en" ' the language param is only available with Plus and Security plans
' Lookup ip address geolocation data
Dim MyTask = IPL.Lookup(IP, Lang) ' async API call
Try
Dim MyObj = MyTask.Result
' pretty-print JSON
Console.WriteLine(JsonConvert.SerializeObject(MyObj, Formatting.Indented))
' individual fields
Console.WriteLine("ip: {0}", MyObj("ip"))
Console.WriteLine("country_code: {0}", MyObj("country_code"))
Console.WriteLine("country_name: {0}", MyObj("country_name"))
Console.WriteLine("region_name: {0}", MyObj("region_name"))
Console.WriteLine("city_name: {0}", MyObj("city_name"))
Console.WriteLine("latitude: {0}", MyObj("latitude"))
Console.WriteLine("longitude: {0}", MyObj("longitude"))
Console.WriteLine("zip_code: {0}", MyObj("zip_code"))
Console.WriteLine("time_zone: {0}", MyObj("time_zone"))
Console.WriteLine("asn: {0}", MyObj("asn"))
Console.WriteLine("as: {0}", MyObj("as"))
If MyObj("isp") IsNot Nothing Then
Console.WriteLine("isp: {0}", MyObj("isp"))
End If
If MyObj("domain") IsNot Nothing Then
Console.WriteLine("domain: {0}", MyObj("domain"))
End If
If MyObj("net_speed") IsNot Nothing Then
Console.WriteLine("net_speed: {0}", MyObj("net_speed"))
End If
If MyObj("idd_code") IsNot Nothing Then
Console.WriteLine("idd_code: {0}", MyObj("idd_code"))
End If
If MyObj("area_code") IsNot Nothing Then
Console.WriteLine("area_code: {0}", MyObj("area_code"))
End If
If MyObj("weather_station_code") IsNot Nothing Then
Console.WriteLine("weather_station_code: {0}", MyObj("weather_station_code"))
End If
If MyObj("weather_station_name") IsNot Nothing Then
Console.WriteLine("weather_station_name: {0}", MyObj("weather_station_name"))
End If
If MyObj("mcc") IsNot Nothing Then
Console.WriteLine("mcc: {0}", MyObj("mcc"))
End If
If MyObj("mnc") IsNot Nothing Then
Console.WriteLine("mnc: {0}", MyObj("mnc"))
End If
If MyObj("mobile_brand") IsNot Nothing Then
Console.WriteLine("mobile_brand: {0}", MyObj("mobile_brand"))
End If
If MyObj("elevation") IsNot Nothing Then
Console.WriteLine("elevation: {0}", MyObj("elevation"))
End If
If MyObj("usage_type") IsNot Nothing Then
Console.WriteLine("usage_type: {0}", MyObj("usage_type"))
End If
If MyObj("address_type") IsNot Nothing Then
Console.WriteLine("address_type: {0}", MyObj("address_type"))
End If
If MyObj("district") IsNot Nothing Then
Console.WriteLine("district: {0}", MyObj("district"))
End If
If MyObj("ads_category") IsNot Nothing Then
Console.WriteLine("ads_category: {0}", MyObj("ads_category"))
End If
If MyObj("ads_category_name") IsNot Nothing Then
Console.WriteLine("ads_category_name: {0}", MyObj("ads_category_name"))
End If
Console.WriteLine("is_proxy: {0}", MyObj("is_proxy"))
If MyObj("continent") IsNot Nothing Then
Dim Continent = MyObj("continent")
Console.WriteLine("continent => name: {0}", Continent("name"))
Console.WriteLine("continent => code: {0}", Continent("code"))
Console.WriteLine("continent => hemisphere: {0}", Continent("hemisphere"))
Console.WriteLine("continent => translation => lang: {0}", Continent("translation")("lang"))
Console.WriteLine("continent => translation => value: {0}", Continent("translation")("value"))
End If
If MyObj("country") IsNot Nothing Then
Dim CounTry = MyObj("country")
Console.WriteLine("country => name: {0}", CounTry("name"))
Console.WriteLine("country => alpha3_code: {0}", CounTry("alpha3_code"))
Console.WriteLine("country => numeric_code: {0}", CounTry("numeric_code"))
Console.WriteLine("country => demonym: {0}", CounTry("demonym"))
Console.WriteLine("country => flag: {0}", CounTry("flag"))
Console.WriteLine("country => capital: {0}", CounTry("capital"))
Console.WriteLine("country => total_area: {0}", CounTry("total_area"))
Console.WriteLine("country => population: {0}", CounTry("population"))
Console.WriteLine("country => tld: {0}", CounTry("tld"))
Console.WriteLine("country => currency => code: {0}", CounTry("currency")("code"))
Console.WriteLine("country => currency => name: {0}", CounTry("currency")("name"))
Console.WriteLine("country => currency => symbol: {0}", CounTry("currency")("symbol"))
Console.WriteLine("country => language => code: {0}", CounTry("language")("code"))
Console.WriteLine("country => language => name: {0}", CounTry("language")("name"))
Console.WriteLine("country => translation => lang: {0}", CounTry("translation")("lang"))
Console.WriteLine("country => translation => value: {0}", CounTry("translation")("value"))
End If
If MyObj("region") IsNot Nothing Then
Dim Region = MyObj("region")
Console.WriteLine("region => name: {0}", Region("name"))
Console.WriteLine("region => code: {0}", Region("code"))
Console.WriteLine("region => translation => lang: {0}", Region("translation")("lang"))
Console.WriteLine("region => translation => value: {0}", Region("translation")("value"))
End If
If MyObj("city") IsNot Nothing Then
Dim City = MyObj("city")
Console.WriteLine("city => name: {0}", City("name"))
Console.WriteLine("city => translation => lang: {0}", City("translation")("lang"))
Console.WriteLine("city => translation => value: {0}", City("translation")("value"))
End If
If MyObj("time_zone_info") IsNot Nothing Then
Dim TimeZone = MyObj("time_zone_info")
Console.WriteLine("time_zone_info => olson: {0}", TimeZone("time_zone_info"))
Console.WriteLine("time_zone_info => current_time: {0}", TimeZone("current_time"))
Console.WriteLine("time_zone_info => gmt_offset: {0}", TimeZone("gmt_offset"))
Console.WriteLine("time_zone_info => is_dst: {0}", TimeZone("is_dst"))
Console.WriteLine("time_zone_info => sunrise: {0}", TimeZone("sunrise"))
Console.WriteLine("time_zone_info => sunset: {0}", TimeZone("sunset"))
End If
If MyObj("geotargeting") IsNot Nothing Then
Dim GeoTarget = MyObj("geotargeting")
Console.WriteLine("geotargeting => metro: {0}", GeoTarget("metro"))
End If
If MyObj("proxy") IsNot Nothing Then
Dim Proxy = MyObj("proxy")
Console.WriteLine("proxy => last_seen: {0}", Proxy("last_seen"))
Console.WriteLine("proxy => proxy_type: {0}", Proxy("proxy_type"))
Console.WriteLine("proxy => threat: {0}", Proxy("threat"))
Console.WriteLine("proxy => provider: {0}", Proxy("provider"))
Console.WriteLine("proxy => is_vpn: {0}", Proxy("is_vpn"))
Console.WriteLine("proxy => is_tor: {0}", Proxy("is_tor"))
Console.WriteLine("proxy => is_data_center: {0}", Proxy("is_data_center"))
Console.WriteLine("proxy => is_public_proxy: {0}", Proxy("is_public_proxy"))
Console.WriteLine("proxy => is_web_proxy: {0}", Proxy("is_web_proxy"))
Console.WriteLine("proxy => is_web_crawler: {0}", Proxy("is_web_crawler"))
Console.WriteLine("proxy => is_residential_proxy: {0}", Proxy("is_residential_proxy"))
Console.WriteLine("proxy => is_spammer: {0}", Proxy("is_spammer"))
Console.WriteLine("proxy => is_scanner: {0}", Proxy("is_scanner"))
Console.WriteLine("proxy => is_botnet: {0}", Proxy("is_botnet"))
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Module
Run the Test Code
Run the code to query data for the 8.8.8.8 IP address and you’ll see the below output. You’ll notice in our code that we are first outputting the raw JSON in pretty-print form. That’s to show you what the actual data coming in from the API will look like. Following that, you will see the output from individual fields. Any fields showing “=>” are nested fields.
{
"ip": "8.8.8.8",
"country_code": "US",
"country_name": "United States of America",
"region_name": "California",
"city_name": "Mountain View",
"latitude": 37.38605,
"longitude": -122.08385,
"zip_code": "94035",
"time_zone": "-08:00",
"asn": "15169",
"as": "Google LLC",
"isp": "Google LLC",
"domain": "google.com",
"net_speed": "T1",
"idd_code": "1",
"area_code": "650",
"weather_station_code": "USCA0746",
"weather_station_name": "Mountain View",
"mcc": "-",
"mnc": "-",
"mobile_brand": "-",
"elevation": 32,
"usage_type": "DCH",
"address_type": "Anycast",
"continent": {
"name": "North America",
"code": "NA",
"hemisphere": [
"north",
"west"
],
"translation": {
"lang": "en",
"value": "North America"
}
},
"district": "Santa Clara County",
"country": {
"name": "United States of America",
"alpha3_code": "USA",
"numeric_code": 840,
"demonym": "Americans",
"flag": "https://cdn.ip2location.io/assets/img/flags/us.png",
"capital": "Washington, D.C.",
"total_area": 9826675,
"population": 331002651,
"currency": {
"code": "USD",
"name": "United States Dollar",
"symbol": "$"
},
"language": {
"code": "EN",
"name": "English"
},
"tld": "us",
"translation": {
"lang": "en",
"value": "United States of America"
}
},
"region": {
"name": "California",
"code": "US-CA",
"translation": {
"lang": "en",
"value": "California"
}
},
"city": {
"name": "Mountain View",
"translation": {
"lang": "en",
"value": "Mountain View"
}
},
"time_zone_info": {
"olson": "America/Los_Angeles",
"current_time": "2023-11-23T12:02:00+08:00",
"gmt_offset": -28800,
"is_dst": false,
"sunrise": "06:56",
"sunset": "16:53"
},
"geotargeting": {
"metro": "807"
},
"ads_category": "IAB19-11",
"ads_category_name": "Data Centers",
"is_proxy": false,
"proxy": {
"last_seen": 22,
"proxy_type": "DCH",
"threat": "-",
"provider": "-",
"is_vpn": false,
"is_tor": false,
"is_data_center": true,
"is_public_proxy": false,
"is_web_proxy": false,
"is_web_crawler": false,
"is_residential_proxy": false,
"is_spammer": false,
"is_scanner": false,
"is_botnet": false
}
}
ip: 8.8.8.8
country_code: US
country_name: United States of America
region_name: California
city_name: Mountain View
latitude: 37.38605
longitude: -122.08385
zip_code: 94035
time_zone: -08:00
asn: 15169
as: Google LLC
isp: Google LLC
domain: google.com
net_speed: T1
idd_code: 1
area_code: 650
weather_station_code: USCA0746
weather_station_name: Mountain View
mcc: -
mnc: -
mobile_brand: -
elevation: 32
usage_type: DCH
address_type: Anycast
district: Santa Clara County
ads_category: IAB19-11
ads_category_name: Data Centers
is_proxy: False
continent => name: North America
continent => code: NA
continent => hemisphere: [
"north",
"west"
]
continent => translation => lang: en
continent => translation => value: North America
country => name: United States of America
country => alpha3_code: USA
country => numeric_code: 840
country => demonym: Americans
country => flag: https://cdn.ip2location.io/assets/img/flags/us.png
country => capital: Washington, D.C.
country => total_area: 9826675
country => population: 331002651
country => tld: us
country => currency => code: USD
country => currency => name: United States Dollar
country => currency => symbol: $
country => language => code: EN
country => language => name: English
country => translation => lang: en
country => translation => value: United States of America
region => name: California
region => code: US-CA
region => translation => lang: en
region => translation => value: California
city => name: Mountain View
city => translation => lang: en
city => translation => value: Mountain View
time_zone_info => olson:
time_zone_info => current_time: 23/11/2023 12:02:00 PM
time_zone_info => gmt_offset: -28800
time_zone_info => is_dst: False
time_zone_info => sunrise: 06:56
time_zone_info => sunset: 16:53
geotargeting => metro: 807
proxy => last_seen: 22
proxy => proxy_type: DCH
proxy => threat: -
proxy => provider: -
proxy => is_vpn: False
proxy => is_tor: False
proxy => is_data_center: True
proxy => is_public_proxy: False
proxy => is_web_proxy: False
proxy => is_web_crawler: False
proxy => is_residential_proxy: False
proxy => is_spammer: False
proxy => is_scanner: False
proxy => is_botnet: False
That’s a lot of data that developers can use. For online merchants, checking the is_vpn and is_residential_proxy fields is vital to prevent bad actors from making fraudulent purchases. If either field shows True, it would be a good idea to reject that online order. As for developers, the wealth of geolocation data provided can be utilized in a multitude of ways to improve your website’s user experience.
Conclusion
Please be aware that our demo above uses a security plan; therefore, all available fields are shown. If you’re not subscribed to the Security plan, you will see less data. But, even with the free plan that only provides basic geolocation data, it’s still pretty useful for developers to enhance their websites’ offerings.
Opinions expressed by DZone contributors are their own.
Comments