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

前端 未结 3 639
心在旅途
心在旅途 2021-02-07 07:05

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-w

3条回答
  •  一整个雨季
    2021-02-07 07:33

    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);
    

提交回复
热议问题