Can I change the POST value without re-POSTing?

This is the use of ASP.NET 2.0 in the IIS 6 world.

I have a user submitting a form that submits data via POST. The page receiving the data performs some simple checks. If the check passes, the black box routine is executed, which basically reads the data using Request.Form ("NameHere").

I would like to be able to change the value of the POST element and then return it back to POST. I have no way to modify the code that reads Request.Form ("NameHere"), so my work around the idea is to change the data during the page load event. If I changed the value of the POST element, then the black box code should not be changed.

Is it possible to change the value of an element in HTTP POST?

Has anyone done this?

Thanks!

+5
source share
3 answers

Even if it's a bit hacked, there is a way to change the value of the POST variable.

We can use Reflection to mark the collection Request.Formas non-readonly, change the value to what we want, and mark it again as read-only (so that other people cannot change the values). Use the following function:

protected void SetFormValue(string key, string value)
{
  var collection = HttpContext.Current.Request.Form;

  // Get the "IsReadOnly" protected instance property.
  var propInfo = collection.GetType().GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

  // Mark the collection as NOT "IsReadOnly"
  propInfo.SetValue(collection, false, new object[] { });

  // Change the value of the key.
  collection[key] = value;

  // Mark the collection back as "IsReadOnly"     
  propInfo.SetValue(collection, true, new object[] { });
}

I tested the code on my machine and it works great. However, I cannot give any guarantees of performance or portability.

+12
source

The current POST cannot be changed, however you can create a new POST request and redirect to it.

+2

I see the only way to change the original POST address to your own, and then in your code all requests arriving at your address are sent to the black box address.

There is some overhead.

As far as I remember, the assembly of forms is not amenable to change, right? I don’t remember the exact structure, but I think that

Request.Form("NameHere") = "newValue"

will not work.

considers

+1
source

All Articles