Visual Basic captcha solver: a VB.NET API client

A Visual Basic captcha solver is a VB.NET console program that posts a challenge to a solver API and reads the answer with HttpClient and System.Text.Json, both part of .NET 6 or newer. With ilovecaptcha it makes two HTTPS calls, createTask then getTaskResult. This page explains the request, the image, the asynchronous entry point and the errors, then gives the complete Program.vb.

Updated on · Written and maintained by the ilovecaptcha team

Preview: the API described here is in public beta. Endpoint names, fields and codes can still change until the end of the beta, and no API key is distributed yet.

How do you call a captcha solver API from Visual Basic?

With HttpClient, declared once as a ReadOnly field of the module with a Timeout of 30 seconds. The function Post writes the key into a JsonObject under clientKey, sends it with PostAsJsonAsync and reads the reply as a JsonNode with ReadFromJsonAsync. The response is released by a Using block.

The Visual Basic example at a glance
Item Value
Runs on VB.NET on .NET 6 or newer
Packages to add None: base class library only
File Program.vb
Create and run dotnet new console -lang VB -o solve && copy Program.vb solve && cd solve && dotnet run
HTTP client HttpClient

How do you send the captcha image and wait for the answer in Visual Basic?

IO.File.ReadAllBytesAsync loads the image and Convert.ToBase64String encodes it. Solve builds the body with a JsonObject collection initializer, posts it to createTask, then awaits Task.Delay before each call to getTaskResult until status is ready.

  • PollInterval is a TimeSpan of 1.5 seconds.
  • MaxWait is a TimeSpan of 120 seconds: past that, Solve throws a TimeoutException.
  • Values are converted with CType: Long for taskId, String for status.
  • The loop is a While block that compares DateTime.UtcNow with the deadline.

Why does Main call MainAsync, and how are errors reported in Visual Basic?

Visual Basic does not accept an Async entry point, so Sub Main starts MainAsync and blocks on it with GetAwaiter and GetResult, which also rethrows its exception unchanged. When errorId is present and not 0, Post throws an InvalidOperationException with errorCode and errorDescription. Read the key with Environment.GetEnvironmentVariable from a variable such as ILOVECAPTCHA_KEY rather than from the constant ApiKey.

What does a complete Visual Basic captcha solver client look like?

The module below is the whole client, in 51 lines. Module Program holds the functions Post, Solve and GetBalance, then Main and MainAsync, which reads captcha.png, submits it as an aws-waf task and prints the solution. Replace YOUR_API_KEY and YOUR_API_URL with your own values and adapt the task object to your captcha before you run it.

Program.vb · Complete Visual Basic client: one createTask call, then getTaskResult until the task is ready (preview format).
Imports System.Net.Http
Imports System.Net.Http.Json
Imports System.Text.Json.Nodes

Module Program
    Const ApiKey As String = "YOUR_API_KEY"
    Const BaseUrl As String = "YOUR_API_URL"
    ReadOnly PollInterval As TimeSpan = TimeSpan.FromSeconds(1.5)
    ReadOnly MaxWait As TimeSpan = TimeSpan.FromSeconds(120)
    ReadOnly Http As New HttpClient With {.Timeout = TimeSpan.FromSeconds(30)}

    Async Function Post(route As String, body As JsonObject) As Task(Of JsonNode)
        body("clientKey") = ApiKey
        Using response = Await Http.PostAsJsonAsync(BaseUrl & route, body)
            Dim result = Await response.Content.ReadFromJsonAsync(Of JsonNode)()
            If result Is Nothing Then Throw New InvalidOperationException("empty response")
            If result("errorId") IsNot Nothing AndAlso CType(result("errorId"), Integer) <> 0 Then
                Throw New InvalidOperationException($"{result("errorCode")}: {result("errorDescription")}")
            End If
            Return result
        End Using
    End Function

    Async Function Solve(captchaTask As JsonObject) As Task(Of JsonNode)
        Dim result = Await Post("/createTask", New JsonObject From {{"task", captchaTask}})
        If CType(result("status"), String) = "ready" Then Return result("solution")
        Dim taskId = CType(result("taskId"), Long)
        Dim deadline = DateTime.UtcNow + MaxWait
        While DateTime.UtcNow < deadline
            Await Task.Delay(PollInterval)
            result = Await Post("/getTaskResult", New JsonObject From {{"taskId", taskId}})
            If CType(result("status"), String) = "ready" Then Return result("solution")
        End While
        Throw New TimeoutException($"task {taskId} not ready after {MaxWait.TotalSeconds} s")
    End Function

    Async Function GetBalance() As Task(Of Decimal)
        Dim result = Await Post("/getBalance", New JsonObject())
        Return CType(result("balance"), Decimal)
    End Function

    Sub Main()
        MainAsync().GetAwaiter().GetResult()
    End Sub

    Async Function MainAsync() As Task
        Dim image = Convert.ToBase64String(Await IO.File.ReadAllBytesAsync("captcha.png"))
        Console.WriteLine(Await Solve(New JsonObject From {{"type", "aws-waf"}, {"image", image}}))
        Console.WriteLine(Await GetBalance())
    End Function
End Module

Visual Basic captcha solver: frequently asked questions

Do I need a NuGet package or an SDK to solve captchas in Visual Basic?

No. HttpClient, PostAsJsonAsync and JsonNode come with .NET 6 or newer, and the three Imports lines at the top of the file are all the module needs. The project created by dotnet new console with the VB language option has no PackageReference to add. The module on this page is the complete client: rename it, keep Post and Solve, and call them from your own code.

Does the example work in VBA or in Visual Basic 6?

No. The example is VB.NET: it uses Async and Await, HttpClient and System.Text.Json, none of which exist in VBA or in Visual Basic 6. From Excel or Access, the same two calls can be made with the COM object WinHttp.WinHttpRequest and a JSON body assembled as a string, which is the approach of the AutoIt example on this site. The sequence stays identical: createTask, then getTaskResult until the task is ready.

What does Solve return in Visual Basic, and how do you read it?

A JsonNode: the solution object of the API. Index it with the key your task type returns and convert the value with CType: String for token or text, Integer for offset. For points, call AsArray and read each pair with two indexes, in pixels of the image you submitted. The example passes the node to Console.WriteLine, which prints its JSON text.

Sources

The facts and figures quoted on this page come from these sources.