Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shamooraee opened this issue Jun 19, 2024 · 4 comments
Closed

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

shamooraee opened this issue Jun 19, 2024 · 4 comments

Comments

@shamooraee
Copy link

@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
Copy link
Author

shamooraee commented Jun 19, 2024

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
Copy link
Author

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

@stratdev3
Copy link

@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
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants