Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the value of a POST value without re-POSTing?

This is using ASP.NET 2.0 in an IIS 6 world.

I have a user submitting a form that sends the data to be via POST. The page receiving the data does some simple validations. If validation passes, then a black-box code routine runs that basically reads the data using Request.Form("NameHere").

I'd like to be able to change the value of the POST item and then put it back on the POST. I don't have the ability to modify the code that reads the Request.Form("NameHere"), so my work around idea is to modify the data during the page's load event. If I change the value of the POST item, then the black-box code doesn't have to be modified.

Is it possible to change the value of an item on the HTTP POST?

Has anyone done this?

Thanks!

like image 595
MADCookie Avatar asked Aug 24 '09 15:08

MADCookie


People also ask

What will happen if we send POST request on GET?

Use GET if you want to read data without changing state, and use POST if you want to update state on the server. Save this answer.

Why POST Over GET?

It's safe to bookmark and refresh GETs, so there's no warning from browsers, etc., etc. That said, there is no security difference - so the username password example you give isn't exactly accurate. POST is as easily tampered with or viewed as GET.


2 Answers

Even though it is a bit hacky, there is a way to change the value of a POST variable.

We can use Reflection to mark the Request.Form collection as non-readonly, change the value to what we want and mark it back as readonly again (so that other people cannot change 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 have tested the code on my machine and it works fine. However, I cannot give any performance or portability guarantees.

like image 200
paracycle Avatar answered Sep 28 '22 09:09

paracycle


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

like image 29
JoshJordan Avatar answered Sep 28 '22 08:09

JoshJordan