' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System.IO
Imports System.Net
Imports System.Threading

Public Partial Class MainForm
    Public Sub New()
        Me.InitializeComponent()
        Dim t As Thread = New Thread(AddressOf WebServer)
        t.Start()
    End Sub
    Private Sub UpdateTextBox(tb As TextBox, msg As String)
        If tb.InvokeRequired Then
            tb.Invoke(DirectCast(Sub() tb.Text = tb.Text + msg + vbCrLf, Action))
        Else
            tb.Text = tb.Text + vbCrLf + msg
        End If
    End Sub
    Public Sub WebServer()
        Dim srv As New HttpListener()
        srv.Prefixes.Add("http://localhost:8888/")
        srv.Start()
        While True
            Dim ctx As HttpListenerContext = srv.GetContext()
            Dim req As HttpListenerRequest = ctx.Request
            Dim resp As HttpListenerResponse = ctx.Response
            For Each k As String In req.Headers.AllKeys
                UpdateTextBox(textBox1, k & "=" &  req.Headers(k))
            Next
            Dim sr As New StreamReader(req.InputStream)
            Dim body As String = sr.ReadToEnd()
            sr.Close()
            UpdateTextBox(textBox2, body)
            Dim sw As New StreamWriter(resp.OutputStream)
            sw.WriteLine("Det virker")
            sw.Close()
        End While
        'srv.Stop();
        'srv.Close();
    End Sub
End Class
