As shown in the previous few posts the HttpListener class can be very useful for handling basic HTTP requests to your custom application. I have shown how you can use the class to respond to basic HTTP requests, but what if you need to request information from a user? Simple, just have your application create a standard HTML page with the necessary form fields. Then you need to decide what method you will use to send that form data back to your application. You have a choice of using GET or POST. The GET method sends all of the form fields across in the querystring of the HTML request. A POST sends the information as a stream of data inside the actual request.
So what is the best method to use, get or post? It really depends on the amount of data you need to send back to your application. If you only have a few fields then "get" will work fine. If you have a large amount of data or you are trying to upload a file you will need to use "post". Most browsers have a size limit for a URL and since "get" uses the querystring you can easily exceed the URL size limit.
As a quick recap of HTML 101; to create a form that uses the "get" method you would use something like:
<form method="get" action="someURL">
And for a post:
<form method="post" action="someURL">
Now that you know the difference between get and post methods for a form, how do we use that with the HttpListener class? If you are using the "get" method it is pretty simple. In the callback method for your HttpListener class you will use the request object to access the querystring property directly. Below is an example for reading a value from the querystring:
string myValue = request.QueryString["myValue"];
As you can see pulling information out of the querystring is very simple and exactly like you would do when working within ASP.NET. Getting data from a form that used the post method is not so straight forward. The request object for the HttpListener class does not contain a Forms method for retrieving the data. We can, however, get access directly to the request object’s data stream and then parse out the form fields using a custom helper method:
private Hashtable GetFormValues(HttpListenerRequest request)
Hashtable formVars = new Hashtable();
//add request data at bottom of page
System.IO.Stream body = request.InputStream;
System.Text.Encoding encoding = request.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
if (request.ContentType.ToLower() == "application/x-www-form-urlencoded")
string s = reader.ReadToEnd();
string pairs = s.Split(‘&’);
for (int x = 0; x < pairs.Length; x++)
string item = pairs[x].Split(‘=’);
A couple things to note about the code sample above; we use the HasEntityBody property of the request object to make sure that we do have some data to work with. Once we know we have data we need to use the ContentType property of the request object to make sure the data that was sent is form field data. It is then a simple process of breaking apart the data and placing it into a Hashtable for use in other parts of the application.
You could also modify the method shown above to also pull out all of the variables in the querystring and place them in the Hashtable. This would then provide you with one single place to go for all of your form data.
for (int x = 0; x < request.QueryString.Count; x++)
To use the GetFormValues method you need to create a new Hashtable object and assign it to the results of the GetFormsValues method. Example:
Hashtable formVals = GetformValues(myRequest);
Now to access a form value you simple use the formVals hashtable:
string myFormValue = formVals["myFormValue"].ToString();
As you can see using a simple helper method you can quickly and easily utilize forms and querystring or form values within your application using the HttpListener class.