Search code examples
c#parsingposthttplistener

Parse through POST


I use Stream reader to read context.Request.InputStream to the end and end up with a string looking like

"Gamestart=true&GamePlayer=8&CurrentDay=Monday&..."

What would be the most efficent/"clean" way to parse that in a C# console?


Solution

  • You can use HttpUtility.ParseQueryString

    Little sample:

    string queryString = "Gamestart=true&GamePlayer=8&CurrentDay=Monday"; //Hardcoded just for example
    NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);
    
    foreach (String k in qscoll.AllKeys)
    {
        //Prints result in output window.
        System.Diagnostics.Debug.WriteLine(k + " = " + qscoll[k]);
    }