I have a HttpListenerRequest which was initiated from a html <form>
that was posted. I need to know how to get the posted form values + the uploaded files. Does anyone know of an example to save me the time doing it for myself? I've had a google around but not found anything of use.
Getting form data from HttpListenerRequest
31.1k Views Asked by Peter Morris AtThere are 3 best solutions below

It's an old thread but I think the demand still applies. In newer versions of .Net (tested on net6.0) it is possible to avoid manual parsing of application/x-www-form-urlencoded content using the standard function System.Web.HttpUtility.ParseQueryString(string content).
One still needs to manually read the content as text from the request input stream, as shown in the accepted answer above.

The main thing to understand is that HttpListener is a low level tool to work with http requests. All post data is in HttpListenerRequest.InputStream stream. Suppose we have a form like that:
<form method=\"post\" enctype=\"multipart/form-data\"><input id=\"fileUp\" name=\"fileUpload\" type=\"file\" /><input type=\"submit\" /></form>
Now we want to see the post data. Lets implement a method to do this:
public static string GetRequestPostData(HttpListenerRequest request)
{
if (!request.HasEntityBody)
{
return null;
}
using (System.IO.Stream body = request.InputStream) // here we have data
{
using (var reader = new System.IO.StreamReader(body, request.ContentEncoding))
{
return reader.ReadToEnd();
}
}
}
upload some file and see result:
Content-Disposition: form-data; name="somename"; filename="D:\Test.bmp"
Content-Type: image/bmp
...here is the raw file data...
Next suppose we have simple form without uploading files:
<form method=\"post\">First name: <input type=\"text\" name=\"firstname\" /><br />Last name: <input type=\"text\" name=\"lastname\" /><input type=\"submit\" value=\"Submit\" /></form>
Let's see the output:
firstname=MyName&lastname=MyLastName
Combined form result:
Content-Disposition: form-data; name="firstname"
My Name
Content-Disposition: form-data; name="somename"; filename="D:\test.xls"
Content-Type: application/octet-stream
...raw file data...
As you can see in case of simple form you can just read InputStream to string and parse post values. If there is a more complex form - you need to perform more complex parsing but it's still can be done. Hope this examples will save your time. Note, that is not always the case to read all stream as a string.
I found a few examples of web servers for MonoTouch but none of them parsed the data sent in a POST request. I looked around the web and was unable to find any examples of how to achieve this. So now that I’ve written it myself I’ve decided to share my own implementation. This includes not only the code for processing the form post data but also for registering request handlers etc.
Here is an example of how you would use the web server
And here are two app specific examples of request handlers
You can download the source code here https://sites.google.com/site/mrpmorris/EmbeddedWebServerMT.zip