Last active
December 19, 2015 00:19
-
-
Save FreekPaans/5867978 to your computer and use it in GitHub Desktop.
Get the full raw http request from an HttpRequestWrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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