Can httpModule "capture" outgoing http requests from a web API hosted on IIS?

311 Views Asked by At

I have created a class library test project that has one class which implements the IhttpModule interface.

I have a demo web api project that is hosted on my local IIS and has an HttpPost method which inside uses a class that makes outgoing Http requests at third parties.

As of now it works like a charm for the incoming requests but i want to catch every request that comes and goes from my API. I do not care about the responses.

I have already checked the requests made at the third parties and they are correct.

API's web.config :

<system.webServer>
  <modules>
    <add name="MyIISModule" type="IisTestProject.MyIISModule" />
  </modules>
</system.webServer>

MyIISModule.cs :

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(OnBeginRequest);
    }


    private void OnBeginRequest(object sender, EventArgs e)
    {
        var app = (HttpApplication) sender;
        File.AppendAllLines("C:\\requestHeaders.txt",  new List<string>() {$"{DateTime.Now.ToOADate()}_{app.Context.Request.ToString()}" });
    }

    public void Dispose()
    {

    }

I made this so i can just check whenever a request happens.

Is there any way to do this without changing my API's code and using only my HttpModule?

Also i have found this:

HTTP modules can only see requests going through IIS/ASP.NET pipeline, while such outbound requests go through Windows sockets directly, and not through IIS/ASP.NET pipeline.

Is there any way to circumvent this?

0

There are 0 best solutions below