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,
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.
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:
If you're using HTTP GET instead of POST:
If you want to avoid the dependency on System.Web that is required to use HttpUtility.ParseQueryString, you could use the
Uri
extension methodParseQueryString
found inSystem.Net.Http
.Make sure to add a reference (if you haven't already) to
System.Net.Http
in your project.Note that you have to convert the response body to a valid
Uri
so thatParseQueryString
(inSystem.Net.Http
)works.