0

I need to read only the mode segment from the below response body.

grant_type=password&username=demouser&password=test123&client_id=500DWCSFS-D3C0-4135-A188-17894BABBCCF&mode=device

I used the below function to read the HTTP body and it gives me the entire body. How to chop the mode segment without using substring or changing the value in seek() : bodyStream.BaseStream.Seek(3, SeekOrigin.Begin);

var bodyStream = new StreamReader(HttpContext.Current.Request.InputStream);
bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);
var bodyText = bodyStream.ReadToEnd();
Harsha W
  • 3,162
  • 5
  • 43
  • 77

1 Answers1

2

You can't. HTTP uses TCP, which requires you to read the entire body anyway, you can't "seek" into a TCP stream. Well, you can, but that still reads the entire body and discards the unused pieces.

So you have to read the entire stream, and you have to meaningfully parse it, because another parameter could also contain the string "mode", and it could also be at the start, so you also can't search for &mode.

Given this is a form post, you can simply access Request.Form["mode"]. If you do want to parse it yourself:

string formData;
using (reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
    formData = reader.ReadToEnd();
}

var queryString = HttpUtility.ParseQueryString(formData);
var mode = queryString["mode"];
CodeCaster
  • 147,647
  • 23
  • 218
  • 272