我们通过分析HttpRequest对象的GetEntireRawContent()方法在读取数据,并且发现这个方法在读取数据时,是将数据赋值到了_rawContent属性,所以我们只要能把处理后的数据赋值给HttpRequest对象的_rawContent属性,即可封送数据给Asp.NET页面。同时,由于我们改写了请求内容,所以HttpRequest的_contentLength也应当改写。
由于这些属性方式是私有的,我们不能直接访问,所以我们必须采用反射的方法给属性赋值。代码如下:
private void InjectTextParts(HttpRequest request, byte[] textParts)
{
BindingFlags flags1 = BindingFlags.NonPublic | BindingFlags.Instance;
Type type1 = request.GetType();
FieldInfo info1 = type1.GetField("_rawContent", flags1);
FieldInfo info2 = type1.GetField("_contentLength", flags1);
if ((info1 != null) && (info2 != null))
{
Assembly web = Assembly.GetAssembly(typeof(HttpRequest));
Type hraw = web.GetType("System.Web.HttpRawUploadedContent");
object[] argList = new object[2];
argList[0] = textParts.Length + 1024;
argList[1] = textParts.Length;
CultureInfo currCulture = CultureInfo.CurrentCulture;
object httpRawUploadedContent = Activator.CreateInstance(hraw,
BindingFlags.NonPublic | BindingFlags.Instance,
null,
argList,
currCulture,
null);
Type contentType = httpRawUploadedContent.GetType();
FieldInfo dataField = contentType.GetField("_data", flags1);
dataField.SetValue(httpRawUploadedContent, textParts);
FieldInfo lengthField = contentType.GetField("_length", flags1);
lengthField.SetValue(httpRawUploadedContent, textParts.Length);
FieldInfo fileThresholdField = contentType.GetField("_fileThreshold", flags1);
fileThresholdField.SetValue(httpRawUploadedContent, textParts.Length + 1024);
info1.SetValue(request, httpRawUploadedContent);
info2.SetValue(request, textParts.Length);
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-281053/ ,如需转载,请注明出处,否则将追究法律责任。