4

In one of my WebAPI Controllers, I'd like to see the original raw HTTP request which was sent by the client (like the stuff you can see on http://web-sniffer.net/):

POST / HTTP/1.1
Host: www.some.host.tld
Connection: keep-alive
Accept-Encoding: gzip[CRLF]
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
field1=FIELD1&field2=FIELD2
Content-type: application/x-www-form-urlencoded
[...]

How can I do this? Both Request and RequestContext don't contain this data and I didn't find any other resource to query for this content.

I tried searching for similar questions on SO, but only found questions for raw content or (if full html) for Java.

In the back of my mind I have a small memory regarding this in combination with the FilterConfig.cs, but sadly nothing specific enough. And with luck someone more knowledgable than me even knows a way to get this information directly.

Edit:

It seems, that it is impossible to capture the raw HTTP request directly. After reading the linked answers, it seems that I have to rebuild the request from several fields.

According to the link from Jeff's comment in his answer, I have to rebuild the original request from a bunch of parsed fields:

  • HttpContext.Current.Request.HttpMethod + .Url + .ServerVariables["SERVER_PROTOCOL"] for the start line.
  • HttpContext.Current.Request.ServerVariables["ALL_RAW"] for the headers
  • Request.Content.ReadAsStreamAsync().Result (don't forget to seek to position 0 before reading) for the content
MilConDoin
  • 734
  • 6
  • 24
  • This 1 line will solve your issue var data = Request.Headers; – Basanta Matia Apr 12 '17 at 12:37
  • @DanEsparza The answer you linked to (14967457) doesn't help. The answer linked by Jeff Davies in his comment to his answer (1038466) put me on the right track. Please remove or edit your duplicate marker, so that it won't lead others astray to a wrong answer (to my question) when reading this. – MilConDoin Apr 13 '17 at 07:39

1 Answers1

0

Install and run Telerik Fiddler. This captures the traffic, decodes HTTPS traffic. It's what I use all the time for this purpose.

  • 3
    I think the OP wants a code solution. – Luke Apr 12 '17 at 12:38
  • 1
    in that case, I guess this question is a duplicate of this: http://stackoverflow.com/questions/1038466/logging-raw-http-request-response-in-asp-net-mvc-iis7 –  Apr 12 '17 at 12:58