If you are talking about the response contents of .NET Framework ASP.NET Web Forms app (not Core) can the following thread of stackoverflow help?
Logging raw HTTP request/response in ASP.NET MVC & IIS7
https://stackoverflow.com/questions/1038466/logging-raw-http-request-response-in-asp-net-mvc-iis7
Below is the HTTP module I have used to obtain the response contents for my application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
// HTTP Module
public class ResponseContentLogHttpModule : IHttpModule
{
public ResponseContentLogHttpModule()
{
}
public String ModuleName
{
get { return "ResponseContentLogHttpModule"; }
}
public void Init(HttpApplication application)
{
application.BeginRequest += this.BeginRequest;
application.EndRequest += this.EndRequest;
}
private void BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string filePath = context.Request.FilePath;
string fileExtension =
VirtualPathUtility.GetExtension(filePath);
// .aspx only
if (fileExtension.Equals(".aspx"))
{
HttpResponse response = context.Response;
var filter = new OutputFilterStream(response.Filter);
response.Filter = filter;
// to use OutputFilterStream object at EndRequest
// set its reference to HttpContext.Items.
context.Items["FilterStream"] = filter;
}
}
private void EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string filePath = context.Request.FilePath;
string fileExtension = VirtualPathUtility.GetExtension(filePath);
// .aspx only
if (fileExtension.Equals(".aspx"))
{
// get OutputFilterStream object stored at BegineRequest above
var filter = (OutputFilterStream)context.Items["FilterStream"];
// get response content
string responseContent = filter.ReadStream();
}
}
public void Dispose() { }
}
// Wrapping filter class
// (copied from stackoverflow)
public class OutputFilterStream : Stream
{
private readonly Stream InnerStream;
private readonly MemoryStream CopyStream;
public OutputFilterStream(Stream inner)
{
this.InnerStream = inner;
this.CopyStream = new MemoryStream();
}
public string ReadStream()
{
lock (this.InnerStream)
{
if (this.CopyStream.Length <= 0L ||
!this.CopyStream.CanRead ||
!this.CopyStream.CanSeek)
{
return String.Empty;
}
long pos = this.CopyStream.Position;
this.CopyStream.Position = 0L;
try
{
return new StreamReader(this.CopyStream).ReadToEnd();
}
finally
{
try
{
this.CopyStream.Position = pos;
}
catch { }
}
}
}
public override bool CanRead
{
get { return this.InnerStream.CanRead; }
}
public override bool CanSeek
{
get { return this.InnerStream.CanSeek; }
}
public override bool CanWrite
{
get { return this.InnerStream.CanWrite; }
}
public override void Flush()
{
this.InnerStream.Flush();
}
public override long Length
{
get { return this.InnerStream.Length; }
}
public override long Position
{
get { return this.InnerStream.Position; }
set
{
this.CopyStream.Position =
this.InnerStream.Position = value;
}
}
public override int Read(byte[] buffer,
int offset, int count)
{
return this.InnerStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
this.CopyStream.Seek(offset, origin);
return this.InnerStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
this.CopyStream.SetLength(value);
this.InnerStream.SetLength(value);
}
public override void Write(byte[] buffer,
int offset, int count)
{
this.CopyStream.Write(buffer, offset, count);
this.InnerStream.Write(buffer, offset, count);
}
}