How can I use an API to detect if an IP address is a VPN in VB.NET?

To detect if an IP address is a VPN using IP2Location.io in VB.NET, you can use the IP2Location.io API to retrieve geolocation and related data.

  1. We'll assume that you have already installed VB.NET and we won't cover that here. You will also need a paid subscription to the Security plan. Subscribe now!
  2. Using Visual Studio, create a new Console App for VB.NET. Or, in the command line, run the below command inside an empty folder to create the project.
    Bash
    
    dotnet new console --language VB
    			
  3. Next, install the Newtonsoft.Json package using Visual Studio. Or, in the command line, run the below command inside the project folder.
    Bash
    
    dotnet add package Newtonsoft.Json
    			
  4. Edit the Program.vb file and replace the code with the below codes.
    VB.NET
    
    Imports System
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    Imports System.Net
    Imports System.Net.Http
    
    Module Program
        Sub Main()
            Dim client As New HttpClient()
            Dim ip = "8.8.8.8"
            Dim key = "YOUR_API_KEY"
            Try
                Dim response As HttpResponseMessage = client.GetAsync("https://api.ip2location.io/?key=" & key & "&ip=" & ip & "&format=json").Result
                If response.StatusCode = HttpStatusCode.OK Then
                    Dim rawjson As String = response.Content.ReadAsStringAsync().Result
                    Dim results As JObject = JsonConvert.DeserializeObject(Of JObject)(rawjson)
                    If results IsNot Nothing Then
                        If results("proxy") IsNot Nothing Then
                            If results("proxy")("is_vpn") Then
                                Console.WriteLine("The IP " & ip & " is a VPN.")
                            Else
                                Console.WriteLine("The IP " & ip & " is NOT a VPN.")
                            End If
                        Else
                            Throw New Exception("ERROR: The is_vpn field requires a paid subscription to the Security plan.")
                        End If
                    Else
                        Throw New Exception("ERROR: Invalid JSON in response.")
                    End If
                ElseIf response.StatusCode = HttpStatusCode.Unauthorized OrElse response.StatusCode = HttpStatusCode.BadRequest Then
                    Dim rawjson As String = response.Content.ReadAsStringAsync().Result
                    If rawjson.Contains("error_message") Then
                        Dim results As JObject = JsonConvert.DeserializeObject(Of JObject)(rawjson)
                        Throw New Exception("ERROR: " & results("error")("error_message").ToString & ".")
                    End If
                    Throw New Exception(rawjson)
                End If
            Catch ex As HttpRequestException
                Throw New Exception(ex.Message)
            End Try
        End Sub
    End Module
    			
  5. In the command line, run the below command.
    Bash
    
    dotnet run
    			

This script will check if the specified IP address is a VPN. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.

Other Languages

Unlock Location Insights For Free

Empower your applications with accurate IP geolocation information now.

Try It for Free