How to reject and HTTP Posts based on content length without parsing the request stream in C#? -


this question has answer here:

some third party posting data our webpage via http post. want set limit 1 kb http posts, if above 1 kb need reject http post. don't want read whole request stream find out content length. there anyway can validate without reading entire request stream?

it depends whether content length in headers or not. if is, can fetch (e.g. httprequest.contentlength). however, don't have specify that.

otherwise, read first 1k , 1 byte:

 byte[] data = new byte[1025];  int length = 0;  using (var stream =  request.getbufferlessinputsream())  {      while (length < validdata.length)      {          int bytesread = stream.read(data, length, data.length - length);          if (bytesread == 0)          {              break;          }          length += bytesread;      }  }  if (length > 1024)  {      // client sent more 1024 bytes of data!  }  // otherwise, use first "length" bytes of data 

note data has size of 1025 try read 1 more byte we're allowing client send.

if client has sent more 1k, we'll still read first 1k - we'll read single unnecessary byte. won't keep reading forever.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -