Skip to content

Instantly share code, notes, and snippets.

@FreekPaans
Last active December 19, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FreekPaans/5867978 to your computer and use it in GitHub Desktop.
Save FreekPaans/5867978 to your computer and use it in GitHub Desktop.
Get the full raw http request from an HttpRequestWrapper
static string GetHttpRequest(HttpRequestBase request) {
using(var stream = new MemoryStream()) {
var writer = new StreamWriter(stream);
writer.Write(request.HttpMethod + " " + request.Path);
var queryStringText = GetRawQueryString(request);
if(!string.IsNullOrEmpty(queryStringText)) {
writer.Write("?"+queryStringText);
}
writer.Write(" "+request.ServerVariables["SERVER_PROTOCOL"]+"\r\n");
writer.Write(request.ServerVariables["ALL_RAW"]);
writer.Write("\r\n");
writer.Flush();
var pos = request.InputStream.Position;
try {
request.InputStream.Position = 0;
request.InputStream.CopyTo(stream);
return Encoding.UTF8.GetString(stream.ToArray());
}
finally {
request.InputStream.Position = pos;
}
}
}
readonly static FieldInfo _httpRequestField = typeof(HttpRequestWrapper).GetField("_httpRequest",BindingFlags.NonPublic|BindingFlags.Instance);
readonly static PropertyInfo _httpRequestQueryStringProperty = typeof(HttpRequest).GetProperty("QueryStringText",BindingFlags.NonPublic|BindingFlags.Instance);
private static string GetRawQueryString(HttpRequestBase request) {
var requestObject = (HttpRequest)_httpRequestField.GetValue(request);
var queryStringText= (string)_httpRequestQueryStringProperty.GetValue(requestObject,null);
return queryStringText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment