HTTP Form, read values?

Hey,

can anybody tell me how i can read the option value?

Thank You in advance.

    void httpListener()
        {
            HttpListener listener = new HttpListener("http", 80);
            listener.Start();
            while (true)
            {
                HttpListenerResponse response = null;
                HttpListenerContext context = null;
                try
                {
                    context = listener.GetContext();
                    response = context.Response;

                    // The button is pressed
                    if (context.Request.HttpMethod == "POST")
                    {
                        // Here i want to set the Color of the multicolourLed depending on the option value
                    }

                    // Sends response
                    response.StatusCode = (int)HttpStatusCode.OK;
                    byte[] HTML = Encoding.UTF8.GetBytes(
                    "<html><body>" +
                    "<h1>FEZ Spider LED CONTROL</h1>" +
                    "<form action=\"\" method=\"post\">" +
                    "<select>" +
                    "<option value=\"blue\">Blue</option>" +
                    "<option value=\"red\">Red</option>" +
                    "<option value=\"green\">Green</option>" +
                    "<option value=\"white\">White</option>" +
                    "</select>" +
                    "<input type=\"submit\" value=\"Activate!\">" +
                    "</form>" +
                    "</body></html>");
                    response.ContentType = "text/html";
                    response.OutputStream.Write(HTML, 0, HTML.Length);
                    response.Close();
                }
                catch
                {
                    if (context != null)
                    {
                        context.Close();
                    }
                }
            }
        }

Add an input in the html :


              <input type="hidden" name="mySelecteIndex" value="$mySelecteIndex$"/>

When you post you page, you replace the value (with the split we tlaked about in the other thread with delimiter ‘$’ in my example) by the select index value of you

And you call a javascript on the ‘Onload’ of the form, which apply the selectindex like this :


    document.getElementById("mySelectObject").selectedIndex = document.myForm.mySelecteIndex.value;

And it is done !

In fact you have to implement the Page parsing as it is done in ASP by IIS for example !

puhh sounds complicated, maybe a bit to hard for me atm,
i have no idea about javascript, ASP, ISS… :smiley:

@ agp89 - ASP and IIS are examples !

If you want to do some things around embedded web site, you’ll have to know a bit on HTML, Javascript and so on…Instead it will rapidly be complicated in fact.

We may help you if you want…Just look at these just before to ensure we will understand each other :

Starting point for JS :

For HTML :

@ agp89 - Now, after reading your post again, what you want to do is to read from the post, which is much simple and only need c# :

This code to replace your comment in the POST


// Get the POST parameters in the Header
if (_request.HttpMethod == "POST" && _request.InputStream != null && _request.InputStream.Length > 0)
{
  _params = new HTTPRequestParamters(GetPostParams(_request.InputStream));
}
else
{
  // POST without params means error !!!
  _params = null;
  _responsebuffer = _pagemanager.GetErrorBody();
}

This code that extract the POST params to a ‘&’ separated string


        private string GetPostParams(System.IO.Stream post)
        {
            string _postparams = "";

            try
            {
                // Get the byte stream
                byte[] requestbuffer = new byte[post.Length];
                post.Read(requestbuffer, 0, (int)post.Length);

                // Convert to string
                char[] charbuf = System.Text.Encoding.UTF8.GetChars(requestbuffer);
                foreach (char car in charbuf)
                {
                    _postparams += car;
                }

                // Return the params
                return _postparams;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

And finally this one which build a DIctionary with the params to facilitate their use


        private ArrayList m_paramlist = new ArrayList();

        /// <summary>
        /// Default constructor that build the dictionary
        /// </summary>
        /// <param name="parameters"></param>
        internal HTTPRequestParamters(string parameters)
        {
            try
            {
                // Split each pair of field/value
                Array _paramarray = parameters.Split('&');

                foreach (string _param in _paramarray)
                {
                    // Split the pair in field and value
                    DictionaryEntry _entry = new DictionaryEntry(_param.Split('=')[0].Trim(), _param.Split('=')[1].Trim());
                    m_paramlist.Add(_entry);
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

hey,

sorry for answering just now, just came back to my computer.

looks like you have put a lot of effort on that, thank you very much, ill try it out now and replay :wink:

PS: the links u posted are quiet helpful as well :slight_smile:

I did !

I embed a website in the board to put some params like TCP Configuration, Password and so on…And it tooks me some time to build something similar to a dynamic web server engine with html and codebehind !

Thank you very much again. You are my hero :slight_smile:

i got it to work, but not exactly how u suggested it, my code looks messy now…

this part:

_params = new HTTPRequestParamters(GetPostParams(_request.InputStream));

what does that line do, i mean the = new HttpRequestParamters part ?

also my compiler wants the method to return a value as it is internal, which is new to me aswell…

and where do i declare _params and what type is it supposed to be?

also how do i then access the list of Dictionary Entry’s… ( first time i heard of them)

I’m sorry for all this questions and that i took so much of your time already, but i only know C# since a few weeks, and just got my Spider but i already learned a lot from u today :slight_smile:

btw, i guess u look exactly how your profile picture when u read all my stupid questions :smiley:

OK, I missed a part of Code, the HttpRequestParamters is for me a Class, but do not take care of internal or other…First just put every method in the same class and call them as it is.

something like that :


_params = BuildPostParams(GetPostParams(_request.InputStream));

// Having :

private  ArrayList BuildPostParams(string parameters)
{
            ArrayList _paramlist = new ArrayList();
            try
            {

                // Split each pair of field/value
                Array _paramarray = parameters.Split('&');
 
                foreach (string _param in _paramarray)
                {
                    // Split the pair in field and value
                    DictionaryEntry _entry = new DictionaryEntry(_param.Split('=')[0].Trim(), _param.Split('=')[1].Trim());
                    _paramlist.Add(_entry);
                }

            return _paramlist;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

Never !

As I always say, there is no stupid questions , but no quetion is stupid !

:slight_smile: okey

how do i then access the list of Dictionary Entry’s?

a Single Dictionary Entry I can acces with _entry.Key or _entry.Value

but _paramlist[0].Key or _paramlist[0].Value doesnt work…

Do i nedd to copy them into an lokal entry first to access them directly?

Here’s.


                foreach (DictionaryEntry entry in _paramlist)
                {
...
                }


@ LouisCpro would it be possible to put up a CodeShare project as an example of how to do this?

@ Duke Nukem - It should, but as It wont have sense to give my entire solution, making a tiny sample project will take a bit time to mount…So if you’re patient, I will probably be able to put something on in the following days…

1 Like

Thread bump as I think getting a sample of this into CodeShare is really important.

@ Duke,

Did not forget but I’m on another project that takes all the time for a while ! Be sure I’ll do ASAP !

Hi Guys !

finally available at : http://www.tinyclr.com/codeshare/entry/676

IF you have any question, do not hesitate …

1 Like