Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .NET ready made method to process response body of a HttpListener HttpListenerRequest body?

Tags:

c#

.net

http

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,

like image 975
Jotham Avatar asked Nov 20 '09 00:11

Jotham


1 Answers

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);
like image 172
Gonzalo Avatar answered Nov 13 '22 02:11

Gonzalo