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

2019-06-21 18:42发布

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!

3条回答
戒情不戒烟
2楼-- · 2019-06-21 18:46

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-06-21 18:50

i see as the only way to change the original POST-destination to your own one and then in your code all requests that go to your address are dispatched to the black-box address.

there is some overhead through this though.

as far as i remember, the form-collection is unmodifiable, right? don't remember the exact structure but i think that

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

isn't going to work.

regards

查看更多
萌系小妹纸
4楼-- · 2019-06-21 18:59

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.

查看更多
登录 后发表回答