tags:

views:

99

answers:

2

I'm using HttpListener to provide a web server to an application written in another technology on localhost. The application is using a simple form submission (application/x-www-form-urlencoded) to make its requests to my software. I want to know if there is already a parser written to convert the body of the html request document into a hash table or equivalent.

I find it hard to believe I need to write this myself, given how much .NET already seems to provide.

Thanks in advance,

+1  A: 

You mean something like HttpUtility.ParseQueryString that gives you a NameValueCollection? Here's some sample code. You need more error checking and maybe use the request content type to figure out the encoding:

string input = null;
using (StreamReader reader = new StreamReader (listenerRequest.InputStream)) {
    input = reader.ReadToEnd ();
}
NameValueCollection coll = HttpUtility.ParseQueryString (input);

If you're using HTTP GET instead of POST:

string input = listenerRequest.Url.QueryString;
NameValueCollection coll = HttpUtility.ParseQueryString (input);
Gonzalo
That's provided Jotham uses `HTTP-GET` and not `HTTP-POST`. Isn't it?
o.k.w
The sample I posted is actually for POST. If you want GET... /me adds that to the example code.
Gonzalo
This still doesn't address what he's asking for: a x-www-form-urlencoded parser, not a query string parser.
nitzmahone
@nitzmahone: really? Read http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1. The data in the POST of x-www-form-urlencoded is the same as the query strinng for an equivalent GET.
Gonzalo
Thanks guys, I was under the impression there was more to the POST body than the identical encoding to the GET query string. My mistake.
Jotham
A: 

The magic bits that fill out HttpRequest.Form are in System.Web.HttpRequest, but they're not public (Reflector the method "FillInFormCollection" on that class to see). You have to integrate your pipeline with HttpRuntime (basically write a simple ASP.NET host) to take full advantage.

nitzmahone