chronoxor / NetCoreServer

Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
https://chronoxor.github.io/NetCoreServer
MIT License
2.63k stars 550 forks source link

Examples HTTP/HTTPS Web Page > Input > Redirect #303

Closed shamooraee closed 2 months ago

shamooraee commented 2 months ago

@chronoxor Hi sorry for posting as issue but please do you have any example of a Http/Https server which will load index.html from folder and display to user, if also passible to have a form with example input when user submits to get the data in c# to process it and redirect user if needed.

Thank you very much!

shamooraee commented 2 months ago

I made this in OnRequestRecieved and it works...

            if (request.Method == "HEAD")
                SendResponseAsync(Response.MakeHeadResponse());
            else if (request.Method == "GET" && request.Url == "/")
            {
                string responseString = @"
                <html>
                    <body>
                        <form action='/' method='post'>
                            <label for='input1'>Input 1:</label>
                            <input type='text' id='input1' name='input1'><br><br>
                            <label for='input2'>Input 2:</label>
                            <input type='text' id='input2' name='input2'><br><br>
                            <input type='submit' value='Submit'>
                        </form>
                    </body>
                </html>";

                SendResponseAsync(Response.MakeGetResponse(responseString, "text/html; charset=UTF-8"));
            }
            else if ((request.Method == "POST") || (request.Method == "PUT"))
            {
                var body = request.Body;
                var parameters = body.Split('&');
                var input1 =  WebUtility.UrlDecode(parameters[0].Split('=')[1]);
                var input2 = WebUtility.UrlDecode(parameters[1].Split('=')[1]);

                Response.Clear();
                Response.SetBegin(302);
                Response.SetHeader("Location", "/success");
                Response.SetBody();

                SendResponseAsync(Response);
            }
            else if (request.Url == "/success")
            {
                string responseString = @"
                <html>
                    <body>
                        <h1>Form Submitted Successfully!</h1>
                        <p>Your data has been saved.</p>
                    </body>
                </html>";

                SendResponseAsync(Response.MakeGetResponse(responseString, "text/html; charset=UTF-8"));
            }
            else if (request.Method == "OPTIONS")
                SendResponseAsync(Response.MakeOptionsResponse());
            else if (request.Method == "TRACE")
                SendResponseAsync(Response.MakeTraceResponse(request));
            else
                SendResponseAsync(Response.MakeErrorResponse("Unsupported HTTP method: " + request.Method));
shamooraee commented 2 months ago

Got it working completely, thanks anyway! If you see any good things to consider in it let me know.

stratdev3 commented 2 months ago

@shamooraee i made a lib which encapsulate NetCoreServer and and bring some features like serving both static files and rest api.

As reading you, it seems it can fit your needs

shamooraee commented 2 months ago

@stratdev3 awesome! Thank you both! Btw.. exactly my thoughts and why I am here at all: https://github.com/stratdev3/SimpleW?tab=readme-ov-file#the-existings-projects