How can I modify a POST request using a custom IHttpModule and an HttpRequest filter?

旧街凉风 提交于 2019-11-29 07:46:02

The answer to part two of this question is to return the modified content size, not the size of the original stream. Behold!

// return bytesRead;
return newByteCountLength;

This question and your answer were really useful for me, however the answer is not the whole story if you're trying to insert larger amounts of data into the stream:

Returning the modified content size is only valid if the data you're inserting into the stream will not take the number of bytes read into the buffer over the count value. If you try to insert too much data, you're either going to find that the buffer object isn't large enough to hold the data you're inserting, or that by writing more than count bytes to the buffer, you're overwriting some data at the end of the buffer that was meant to be left alone.

If you need to insert more data than the current buffer object can accommodate then you'll have to buffer the data in a separate byte array and copy it in chunks as the calls to stream.read are made.

Snives

As Chris McKeown states, this is not really the best technique for filtering unless you promise to not modify the size of the data. For completeness of this answer, I've posted a sample project which demonstrates how to handle filtering the request and the response using the buffered technique if you are interested.

https://github.com/snives/HttpModuleRewrite

Also for questions relating to troubleshooting HttpModules for filtering this post was also very helpful Is it possible to modify the content of HttpRequest POST in an IIS HttpModule?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!