public static HttpContext GetHttpContext( string queryString = "" ) { HttpRequest request = new HttpRequest( "", "http://www.test.ing", queryString ); var httpContext = new HttpContext( request, new HttpResponse( new StringWriter() ) ); var field = request.GetType().GetField( "_referrer", BindingFlags.Instance | BindingFlags.NonPublic ); field.SetValue( request, new Uri( "http://localhost" ) ); var sessionContainer = new HttpSessionStateContainer( "id", new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 10, true, HttpCookieMode.AutoDetect, SessionStateMode.InProc, false ); httpContext.Items[ "AspSession" ] = typeof ( HttpSessionState ) .GetConstructor( BindingFlags.NonPublic | BindingFlags.Instance, null, CallingConventions.Standard, new[] { typeof ( HttpSessionStateContainer ) }, null ) .Invoke( new object[] { sessionContainer } ); return httpContext; }
/// <summary> /// Dumps the web request /// </summary> /// <param name="request">The request to dump.</param> /// <returns>A string with all the object information in it.</returns> public static string BuildTraceDump(this HttpRequest request) { Check.Require(request, "request"); StringBuilder text = new StringBuilder(); text.AppendFormat("HttpRequest Type: {0}\n", request.GetType().ToString()); text.AppendFormat("ContentLength: {0}\n", request.ContentLength); text.AppendFormat("ContentType: {0}\n", request.ContentType); text.AppendFormat("HttpMethod: {0}\n", request.HttpMethod); text.AppendFormat("RawUrl: {0}\n", request.RawUrl); return(text.ToString()); }
/// <summary> /// Updates the RawUrl property of <see cref="HttpRequest"/> instance. /// </summary> /// <param name="request">The request.</param> /// <param name="url">The target URL.</param> private static void UpdateRequestRawUrl(HttpRequest request, string url) { var rawUrlProperty = request.GetType().GetProperty("RawUrl"); rawUrlProperty.SetValue(request, url, null); }
/// <summary> /// 上传完成后,重写请求头 /// </summary> /// <param name="request">请求头</param> /// <param name="textParts">去掉文件内容的请求头</param> 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 fileCompleted = contentType.GetField("_completed", flags1); fileCompleted.SetValue(httpRawUploadedContent, true); FieldInfo fileThresholdField = contentType.GetField("_fileThreshold", flags1); fileThresholdField.SetValue(httpRawUploadedContent, textParts.Length + 1024); info1.SetValue(request, httpRawUploadedContent); info2.SetValue(request, textParts.Length); } }
/// <summary> /// Checks the properties of the HttpRequest instance passed /// into the method for the property name contained in the property parameters /// string value. /// </summary> /// <param name="property">Property name to be found.</param> /// <param name="request">HttpRequest to be used.</param> /// <returns>If the property exists then return the associated value, otherwise null.</returns> private static string GetHttpRequestPropertyValue(string property, HttpRequest request) { // Try the properties of the request. Type controlType = request.GetType(); System.Reflection.PropertyInfo propertyInfo = controlType.GetProperty(property); if (propertyInfo != null && propertyInfo.CanRead) return propertyInfo.GetValue(request, null).ToString(); return null; }
void InjectTextParts(HttpRequest request, byte[] textParts) { BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic; Type type = request.GetType(); FieldInfo contentField; FieldInfo contentLengthField; // Mono contentField = type.GetField("_arrRawContent", bindingFlags); // ASP.NET if (contentField == null) contentField = type.GetField("_rawContent", bindingFlags); // Mono contentLengthField = type.GetField("_iContentLength", bindingFlags); // ASP.NET if (contentLengthField == null) contentLengthField = type.GetField("_contentLength", bindingFlags); if (contentLengthField != null) contentLengthField.SetValue(request, textParts.Length); if (contentField != null && contentField.FieldType == typeof(byte[])) contentField.SetValue(request, textParts); }
void InjectTextPartsIIS7(HttpRequest request, byte[] textParts) { object content = TypeFactory.CreateInstance( "System.Web.HttpRawUploadedContent, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", new object[] { textParts.Length, textParts.Length }, BindingFlags.NonPublic | BindingFlags.Instance); Type contentT = content.GetType(); contentT.InvokeMember("AddBytes", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, content, new object[] { textParts, 0, textParts.Length }); contentT.InvokeMember("DoneAddingBytes", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, content, null); Type requestT = request.GetType(); requestT.InvokeMember("_rawContent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField, null, request, new object[] { content }); }