Tracking HTTP requests and responses - c#

Is it possible to get all HTTP requests of a browser?
For example: we have opened the browser, navigated to google.com, searched for a string, clicked on any link and I got some error.
Now I want to track all HTTP requests from opening browser.
We have been using 'fiddler' to do so. But we want to use C# code to track all HTTP requests as well as HTTP responses. And we want to use the failed HTTP responses in our program.
Any information on this will be very helpful.

You could use SharpPcap to implement your own 'sniffer', or use a HttpListener to create a proxy that forwards web requests.

AFAICS, there is no way of implementing this which does not replicate what you are already doing with fiddler (albeit that you could implement a passive sniffer rather than a proxy).

Related

View contents of HttpWebRequest without tools(C#)

I want to send a put request with some json strings to a server some time in the future. I want to compare the request Im sending to the requirements I have been given, but I do not have access to that server yet so I cant test if the request Im sending is looking the way it should. The only thing I found would be ToString'ing my httpWebRequest (like in this), which is not what I need.
Is there a way to read exactly what I'm sending after (or before) I send it into the void? Or alternatively, is there a way to send a request to myself and read it that way?
Edit: I cannot install foreign software on my workstation. While using a tool like Wireshark probably solves the answer for the general public, I still need a way to do this programmatically.
Edit2: Searching for the topic may have not resulted in anything, but a random topic on the right that SO suggested actually has (almost exactly) what I wanted and all it takes is copy pasting something into the web.config file. Implementing this answer puts (amongst other things) all the connections as well as their contents into the debug console.
Try using an HTTP debugging tool like Fiddler. It acts like a proxy and can let you view the request and response of HTTP requests your program sends.
Here's how to use it with HttpWebRequest: Get HTTP requests and responses made using HttpWebRequest/HttpWebResponse to show in Fiddler
If you aren't allowed to install Fiddler you could think about making your own version of Fiddler in c# to capture traffic How to create a simple proxy in C#?

WPF receive GET request's query string sent to localhost socket [duplicate]

I have a C# form application which I want to have listening for incoming HTTP requests from other computers.
How would I go about doing this?
For simple needs, the HttpListener class is a good and simple choice. There is an example on the linked MSDN page.
If, for some reason, you cannot use HttpListener, the process would be to listen to a port using TcpClient (or even the sockets API if you need the gritty details), and then implement the HTTP Protocol. I highly recommend HttpListener over rolling your own, unless you have specific requirements that HttpListener does not meet.
You can use ASP.NET Http Filters to intercept HTTP requests.
See more details here
If it's an asp.net application, you can check the http requests in the Application_BeginRequest event handler of your global.asax.

Intercept HTTP requests

I've some fishy application that makes HTTP requests to a website, i would like to intersect that request and send other data to the server. Is that possible in C#,java or C++?
EDIT: The application isn't mine, i just know the endpoint that it sends http requests
Fiddler might provide the functionality you need. At the very least it may enable you to see what is being sent to the web site.
in Java You can intercept request from Filter
You may want to look into using an HttpModule, whose purpose is to intercept incoming HTTP requests.
The ASP Column: HTTP Modules
Firstly are you aware of how it is connecting to the internet? For example, is it using the settings from Internet Explorer, or is it establishing a direct connection? If the latter, this may be tricky, there is no direct port forwarding as there in Linux, so you'll need some third-party tools to redirect the traffic to a server (which you can write in Java, C++ or C#, I would go for C# if you know it for pure speed of development) In that server you can intercept the request, and then create your own to actually send to the real destination.
Sounds like a cludge, but I think you're stuck with this approach due to the lack of direct port forwarding. You'll have to configure the third-party tool that you use to forward someother well known port to 80, and your server should write to this.

Capture http data using System.Windows.Forms.WebBrowser

I'm writing an app with the WebBrowser control and wanted to know how to retrieve the http traffic via the WebBrowser control. post, gets, etc.
Thanks
Use the WebClient class or the underlying WebRequest based classes if you need to programmically make requests.
If you are interested in monitoring it for debug purposes then get fiddler which is a free HTTP debugging proxy.

Can an ASP.NET HttpHandler handle an http 400 - Bad Request?

We have an HttpHandler that deals directly with binary posts over HTTP from custom client software. The client software occasionally sends data which results in IIS 7 responding with a 400 - Bad Request. Since the "400 Bad Request" is special in that HTTP.SYS transparently handles it in kernel mode without notifying user mode of anything, no errors are raised to be handled in ASP.NET. Is it possible to catch this http 400 in ASP.NET, so that I can write specific data to the Response stream in these scenarios? A redirect to another page isn't an option as it needs to be in the current Request/Response.
If you know what is causing the 400, then you may be able to customise the behaviour of http.sys via the registry to deal with it:
http://support.microsoft.com/kb/820129
However, you should be aware that there are potential security and performance ramifications of doing this.
Another option would be to use a filtering proxy before IIS, thereby capturing the request before it goes any further.
I would instead ask them to fix the custom client software. Give them a report showing the failed requests. If you are able to, run a sniffer such as Wireshark and send them the packets if they do not believe the problem is with their software.
If your custom client causes IIS to trigger HTTP 400, it's probably flawed and is not submitting valid HTTP requests according to the standard. If you can alter the client, it would be the right thing to do. Otherwise, what you're working with is not HTTP, and IIS is designed to handle HTTP requests. Therefore, you should run a custom server for your own protocol (which is a non-standard HTTP like thing).
It's not advised to use IIS/ASP.NET to handle such a request as it might cause some weird unexpected things to happen.

Categories