Is there any HTTP server samples?

Is there any sample on how to use the HTTP server to create forms and read the form data when the user clicks a submit button?

I also need to have the WiFi setup as an APN to allow me to do the initial setup and input the selected network APN and password.

I need basic setup for this NixieClock to set the wifi credentials and Timezone etc.

1 Like
using GHIElectronics.TinyCLR.Networking.Http;
using System.Net;
using System.Text;

// ... (other using statements and namespace)

public class WebServerExample
{
    private static HttpListener _listener;
    private static int _clientRequestCount = 0;

    public static void StartWebServer()
    {
        _listener = new HttpListener();
        _listener.Prefixes.Add("http://+:80/"); // Listen on port 80 for all IP addresses
        _listener.Start();

        while (true)
        {
            HttpListenerContext context = _listener.GetContext();
            ProcessRequest(context);
        }
    }

    private static void ProcessRequest(HttpListenerContext context)
    {
        HttpListenerRequest request = context.Request;
        HttpListenerResponse response = context.Response;

        string responseString = "";

        if (request.HttpMethod == "GET")
        {
            // Serve the HTML form
            responseString = @"
                <HTML><HEAD><TITLE>TinyCLR Form</TITLE></HEAD>
                <BODY>
                    <h1>Control LED</h1>
                    <form method=""POST"" action=""/"">
                        <input type=""checkbox"" name=""ledState"" value=""on""> Turn LED On<br>
                        <input type=""submit"" value=""Submit"">
                    </form>
                </BODY></HTML>";
        }
        else if (request.HttpMethod == "POST")
        {
            // Process form submission
            string requestBody = new StreamReader(request.InputStream).ReadToEnd();
            if (requestBody.Contains("ledState=on"))
            {
                // Logic to turn on LED (e.g., control a GPIO pin)
                responseString = "<HTML><BODY>LED is ON!</BODY></HTML>";
            }
            else
            {
                // Logic to turn off LED
                responseString = "<HTML><BODY>LED is OFF!</BODY></HTML>";
            }
        }

        byte[] buffer = Encoding.UTF8.GetBytes(responseString);
        response.ContentLength64 = buffer.Length;
        response.OutputStream.Write(buffer, 0, buffer.Length);
        response.OutputStream.Close();
    }
}

Caution … AI generated

1 Like

Thanks Mike, It almost works and I can see how to do this now. It however hangs at this line and never returns;

string requestBody = new StreamReader(request.InputStream).ReadToEnd();

The request inputStream indicates data length is 0

I suspect the html for the form is wrong. I am html ignorant.

I googled a sample html form with a button and got something different from my original sample.

1 Like

I changed the code to the following and it works and I get back the form inputs.

            char[] msg = new char[request.InputStream.Length];

            new StreamReader(request.InputStream).Read(msg, 0, (int) request.InputStream.Length);

            string requestBody = new string(msg);

I don’t think ReadToEnd is supported.

1 Like