We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,011 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Handling POST HttpListener c#

Hi everyone.

I have a web server that I wrote in C#, and it serves HTML documents pretty fine. The code is a bit messy, and I'm going to clean it up, but my main question is how to handle POST when submit buttons are used in HTML?

Thanks!

This is my code so far:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Reflection;
using System.IO;

namespace CoolioServerWeb
{
    class Program
    {
        public static HttpListener listener = new HttpListener();
        public static string startUpPath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        public static List<FileName> virtualPaths = new List<FileName>();
        public static void Main(string[] args)
        {
            listener.Start();
            load();
            Thread t = new Thread(new ThreadStart(clientListener));
            t.Start();
            Console.Write(">");
            while (true)
            {
                string s = Console.ReadLine();
                Console.Write(">");
            }
        }
        public static void load()
        {
            StreamReader sr = new StreamReader(startUpPath + "\\site.cfg");
            string s = sr.ReadLine();
            while (s != null)
            {
                if (s.StartsWith("Port: "))
                {
                    s = s.Replace("Port: ", "");
                    listener.Prefixes.Add(s);
                }
                s = sr.ReadLine();
            }
        }
        public static void clientListener()
        {
            while (true)
            {
                try
                {
                    HttpListenerContext request = listener.GetContext();
                    ThreadPool.QueueUserWorkItem(processRequest, request);
                }
                catch (Exception e) { Console.WriteLine(e.Message); Console.Write(">"); }
            }
        }
        public static void processRequest(object listenerContext)
        {
            try
            {
                var context = (HttpListenerContext)listenerContext;
                string filename = Path.GetFileName(context.Request.RawUrl);


                string path = "";
                filename = filename.ToLower();
                if (filename.EndsWith(".png") || filename.EndsWith(".jpg") || filename.EndsWith(".gif"))
                {
                    path = startUpPath + "\\webroot\\imgs\\" + filename;
                }
                else if (filename.EndsWith(".htm") || filename.EndsWith(".html") || filename.EndsWith(".js") || filename.EndsWith(".css"))
                {
                    path = startUpPath + "\\webroot\\" + filename;
                }
                else if (filename == "")
                {
                    path = startUpPath + "\\webroot\\index.html";
                }
                
                byte[] msg;
                if (!File.Exists(path))
                {
                    context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    msg = File.ReadAllBytes(startUpPath + "\\webroot\\error.html");
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                    msg = File.ReadAllBytes(path);
                }
                context.Response.ContentLength64 = msg.Length;
                using (Stream s = context.Response.OutputStream)
                    s.Write(msg, 0, msg.Length);
            }
            catch
            {

            }
        }
    }
    class FileName
    {
        public string absolutePath = "";
        public string externalName = "";
    }
}
2
Contributors
1
Reply
4 Days
Discussion Span
1 Year Ago
Last Updated
2
Views
dreadiscool
Newbie Poster
3 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What do you mean, "how to handle POST when submit buttons are used"?
Are you asking how to get values sent by the user?
On a GET, the values are sent through the Query String.
On a POST, the values are sent through environment variables.

...or are you asking something else?

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0735 seconds using 2.69MB