Ahmed
Ahmed

Reputation: 1590

C# Intercept Http Request

I have a .Net application (console or web), I need to create a Helper class contains an event in C# to fire automatically And log request and response when I call a restful service (Within my application not external application) I prefer using a native framework not a library. Is that possible ?

Upvotes: 2

Views: 14583

Answers (3)

Daniel Paiva
Daniel Paiva

Reputation: 121

You can create a middleware to intercept the HTTP processing. I know it should be fairly straight forward in ASP.NET Core and I just found something along those lines for .NET.

https://www.azurefromthetrenches.com/capturing-and-tracing-all-http-requests-in-c-and-net/

In the link above you will find how to listen to events in .NET. You test the event type and if they are of the desired type you will be able to do your custom processing, capturing, tracking etc.

This question is 3 years old so it might not help you but someone else that lands here like me. Also, it would be nice if you could share what worked for you.

Upvotes: 1

V. Kasparavičius
V. Kasparavičius

Reputation: 76

If you have no control over the application and service, you could setup a proxy server in C# and route that other application traffic through it (either using that application or global windows/linux/wtw settings). In C# it could be done using HttpListener (for listening) and HttpWebRequest to forward the requests. An example of full - featured proxy server: Titanium Web Proxy.

If you have control over either of them, something like this could be used.

Upvotes: 1

meJustAndrew
meJustAndrew

Reputation: 6603

You can create your own event and trigger it on the method calls to the service. So yes, it is possible. You will have to create a service caller and your own event. On each method from the service caller, trigger the event. I am not going to add the code or samples on how to create an event. You should be doing the research yourself.

Upvotes: 0

Related Questions