2

By default HttpWebRequest has AllowWriteStreamBuffering set to true, which means that all data written to the request stream is buffered inside the object.

I'd like to access this buffer of data after it has been written, but can't seem to find any way to do so. I'm happy for it to fail if AllowWriteStreamBuffering is false.

Is there any way to do this, or is it not exposed anywhere?


As to why I want to do this: I'm writing an OAuth request signing class, and unfortunately the protocol requires any form-encoded body to be considered part of the signature. So I need to be able to access the body if it's a form encoded one.

Greg Beech
  • 133,383
  • 43
  • 204
  • 250
  • You might download the .NET framework source code and see if there is anything in the HttpWebRequest that gives you access to the buffer: http://referencesource.microsoft.com/netframework.aspx – AaronLS Apr 08 '10 at 13:53
  • @AaronLS - I've been having a look with Reflector but the code is somewhat convoluted to say the least. I was hoping somebody might know the answer for sure (which I suspect is "no, you can't access it"). – Greg Beech Apr 08 '10 at 14:04
  • The only other thing I could think of would be the GetRequestStream method if it is being buffered by the stream. You could use reflection to determine the exact type of Stream it is and hope that type has access to the internal buffer like MemoryStream does. If the buffering is not handled by the stream though this is a dead end. – AaronLS Apr 08 '10 at 14:48

1 Answers1

0

AllowWriteStreamBuffering uses an internal mechanism. The buffer is not exposed to the caller. It allows scenarios where the initial request is redirected, or denied for authentication, in which case the WebRequest can autopost the data to the new endpoint, without having to fail the request and asking the caller to resubmit.

You should find a different way to get the stream. Maybe you can first buffer it into your own buffer (i.e MemoryStream). Do your necessary operations, then flush that stream onto the WebRequest's requestStream.

feroze
  • 7,380
  • 7
  • 40
  • 57