Http Server in Visual Basic .NET

Here’s the translation of the HTTP server example from Go to Visual Basic .NET:

Our HTTP server example demonstrates how to create a basic web server using the System.Net.HttpListener class in Visual Basic .NET.

Imports System
Imports System.Net
Imports System.Text

Module HttpServer
    Sub Hello(context As HttpListenerContext)
        ' Functions serving as handlers take a HttpListenerContext as an argument.
        ' The response is used to send the HTTP response.
        ' Here our simple response is just "hello\n".
        Dim response = context.Response
        Dim responseString = "hello" & Environment.NewLine
        Dim buffer = Encoding.UTF8.GetBytes(responseString)
        response.ContentLength64 = buffer.Length
        response.OutputStream.Write(buffer, 0, buffer.Length)
        response.OutputStream.Close()
    End Sub

    Sub Headers(context As HttpListenerContext)
        ' This handler does something a little more sophisticated by reading
        ' all the HTTP request headers and echoing them into the response body.
        Dim request = context.Request
        Dim response = context.Response
        Dim responseString As New StringBuilder()

        For Each key In request.Headers.AllKeys
            For Each value In request.Headers.GetValues(key)
                responseString.AppendLine($"{key}: {value}")
            Next
        Next

        Dim buffer = Encoding.UTF8.GetBytes(responseString.ToString())
        response.ContentLength64 = buffer.Length
        response.OutputStream.Write(buffer, 0, buffer.Length)
        response.OutputStream.Close()
    End Sub

    Sub Main()
        ' We create an HttpListener and set up our server routes
        Dim listener As New HttpListener()
        listener.Prefixes.Add("http://localhost:8090/")
        listener.Start()

        Console.WriteLine("Listening on http://localhost:8090/")

        While True
            Dim context = listener.GetContext()
            Select Case context.Request.Url.LocalPath
                Case "/hello"
                    Hello(context)
                Case "/headers"
                    Headers(context)
                Case Else
                    ' Handle 404 Not Found
                    context.Response.StatusCode = 404
                    context.Response.Close()
            End Select
        End While

        ' Note: This code will never be reached as the loop above is infinite.
        ' In a real application, you'd want to implement a way to stop the server gracefully.
        listener.Stop()
    End Sub
End Module

To run the server:

$ vbc HttpServer.vb
$ mono HttpServer.exe

Access the /hello route:

$ curl http://localhost:8090/hello
hello

This example demonstrates how to create a simple HTTP server in Visual Basic .NET. It uses the HttpListener class to handle incoming HTTP requests. The Hello and Headers subroutines serve as handlers for different routes.

The Main subroutine sets up the server to listen on http://localhost:8090/ and enters an infinite loop to process incoming requests. Depending on the requested URL path, it calls the appropriate handler.

Note that this is a basic example and doesn’t include error handling or proper server shutdown mechanisms. In a production environment, you’d want to add these features and consider using more robust web frameworks like ASP.NET.