Building an HTTP Listener Server and Client with DSAPI

This article demonstrates how to quickly establish server and client functionality using the DSAPI.Network-related HTTP Listener component.

The HTTP Listener Server monitors specified computer ports to provide web request parsing services similar to IIS. While commonly used for web pages, this functionality extends to various other applications.

The DSAPI.Network HTTP Listener supports events and can be declared using the WithEvents keyword to enable event handling capabilities.

Creating an HTTP Listener Server

With httpServer .BindPort = 2000 ' Port to listen on .ParameterPrefix = "DSAPI" ' String to append when using parameter prefix .AllowExternalConnections = True ' Whether to allow external network connections .UseParameterPrefix = True ' Whether to use parameter prefix .StartListening() ' Begin monitoring End With


</div>The parameter prefix serves as a security measure against malicious requests. When enabled, this prefix is appended to the domain name. For example, if a client requests `http://127.0.0.1:2000/DSAPI`, it is considered a valid request; otherwise, the server will block and discard it.

If parameter prefix is disabled, any request to `http://127.0.0.1:2000/` is accepted regardless of what follows. However, such requests will not be processed.

The "Allow External Connections" option, when enabled, restricts incoming requests to clients with in the same network segment. All other requests are blocked or discarded.

Supported Events
----------------

<div>```
Private Sub httpServer_ListeningStarted() Handles httpServer.ListeningStarted
    ' Triggered when the server starts listening successfully
End Sub

Private Sub httpServer_ListeningStopped() Handles httpServer.ListeningStopped
    ' Triggered when the server stops listening
End Sub

Private Sub httpServer_BlockedClientRequest(client As DSAPI.NetworkRelated.HTTPListener.ListeningClient) _
    Handles httpServer.BlockedClientRequest
    ' Triggered when invalid HTTP requests are received
End Sub

Private Sub httpServer_ClientRequestReceived(client As DSAPI.NetworkRelated.HTTPListener.ListeningClient) _
    Handles httpServer.ClientRequestReceived
    ' Triggered when valid client HTTP requests with data are received
End Sub

Private Sub httpServer_ErrorOccurred(error As Exception) Handles httpServer.ErrorOccurred
    ' Triggered when unexpected issues occur during startup, operation, or shutdown
End Sub

ListeningStopped: Fires when the server halts monitoring.

BlockedClientRequest: Fires when illegitimate HTTP requests are detected, such as external network requests when external connections are disabled, or requests without the correct prefix in the URL when prefix validation is active.

ClientRequestReceived: Fires when a client's HTTP request is valid and contains data.

ErrorOccurred: Fires when unexpected issues arise during server initialization, operation, or termination.

The client request event provides a class封装了客户端请求信息 that contains all information related to that particular client.

Responding to Valid Client Requests

Select Case requestData
    Case "hello"
        httpServer.WriteToClientOutputStream(client, "Hello, my friend")
    Case "get"
        httpServer.WriteToClientOutputStream(client, IO.File.ReadAllBytes("c:\1.rar"))
    Case Else
        httpServer.WriteToClientOutputStream(client, "No command found")
End Select

End Sub


</div>In the code above, "hello" and "get" are read from the client's input stream. These values are not directly visible in the URL, ensuring security.

Creating an HTTP Request Client
-------------------------------

You can use a browser as the client, or create a custom HTTP client:

<div>```
Private Function SendRequest(targetUrl As String, port As String, prefix As String, payload As String) As String
    ' Build and send HTTP request
    Dim request As Net.HttpWebRequest = Net.HttpWebRequest.Create(targetUrl & ":" & port & "/" & prefix)
    Dim payloadBytes() As Byte = System.Text.Encoding.Default.GetBytes(payload)
    
    request.Method = "POST"
    request.ContentLength = payloadBytes.Length
    request.ContentType = "text/xml"
    request.GetRequestStream.Write(payloadBytes, 0, payloadBytes.Length)
    
    ' Retrieve response
    Dim responseReader As New IO.StreamReader(request.GetResponse.GetResponseStream)
    Dim responseData As String = responseReader.ReadToEnd()
    responseReader.Close()
    
    Return responseData
End Function

Method 1 - Using the function:

MsgBox(SendRequest("http://127.0.0.1", 2000, "DSAPI", "hello"))

Method 2 - Direct browser access:

Open your browser and navigate to http://127.0.0.1:2000/DSAPI. Note that browsers cannot manually submit content, so the server should check whether the request payload is emmpty. This method can return web page data for direct display in the client browser.

Tags: DSAPI HTTP Server HTTP Client VB.NET Network Programming

Posted on Sat, 06 Jun 2026 17:06:11 +0000 by carsale