How to Build a Super Simple HTTP Proxy in Visual Basic in just 20 lines of code

Oct 1, 2023 ยท 3 min read

Visual Basic provides a straightforward way to build network applications using the .NET framework. Here is a basic HTTP proxy server in Visual Basic in less than 20 lines of code.

First we import the required namespaces:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading

Next we create a TcpListener to receive incoming connections:

Dim listener As New TcpListener(8080)
listener.Start()

In a loop, we accept a connection:

Dim client As TcpClient = listener.AcceptTcpClient()

And handle the request:

Dim request As String = GetRequest(client)
Dim url As String = ParseUrl(request)

Dim response As Byte() = GetWebPage(url)

SendResponse(client, response)

The GetRequest method reads the request string from the client stream.

We parse out the URL using a regex:

Private Function ParseUrl(request As String) As String
  Return Regex.Match(request, "http://([\\w\\d.]+)/?").Groups(1).Value
End Function

GetWebPage downloads the proxied response:

Private Function GetWebPage(url As String) As Byte()
  Using webClient As New WebClient()
    Return webClient.DownloadData(url)
  End Using
End Function

And SendResponse writes the response back to the client.

The full basic proxy code:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading

Module Proxy
  
  Sub Main()
  
    Dim listener As New TcpListener(8080)
    listener.Start()

    While True
      Dim client As TcpClient = listener.AcceptTcpClient()

      Dim request As String = GetRequest(client)  
      Dim url As String = ParseUrl(request)

      Dim response As Byte() = GetWebPage(url)

      SendResponse(client, response)
    End While

  End Sub

  Private Function GetRequest(client As TcpClient) As String
    ' Read request from client stream
  End Function
  
  Private Function ParseUrl(request As String) As String
    Return Regex.Match(request, "http://([\w\d.]+)/?").Groups(1).Value 
  End Function

  Private Function GetWebPage(url As String) As Byte() 
    Using webClient As New WebClient()
      Return webClient.DownloadData(url) 
    End Using
  End Function

  Private Sub SendResponse(client As TcpClient, response As Byte())
    ' Write response back to client
  End Sub

End Module

This shows how to build a simple proxy in Visual Basic using .NET's socket classes.

This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.

Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.

Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

  • With millions of high speed rotating proxies located all over the world,
  • With our automatic IP rotation
  • With our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)
  • With our automatic CAPTCHA solving technology,
  • Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

    The whole thing can be accessed by a simple API like below in any programming language.

    In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so:

    curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"
    
    

    We have a running offer of 1000 API calls completely free. Register and get your free API Key.

    Browse by language:

    Tired of getting blocked while scraping the web?

    ProxiesAPI handles headless browsers and rotates proxies for you.
    Get access to 1,000 free API credits, no credit card required!