How can I trace the HttpClient request using fiddler or any other tool?
Asked Answered
G

7

54

I am using HttpClient for sending out request to one of the web api service that I don't have access to and I need to trace the actual request stream getting to the server from my client. Is there a way I can hookup the Fiddler to listen to the requests?

I am using the System.Net.Http.HttpClient class for sending out request.

Update: trying to improve this question now as I could not get what I was looking for. I am using a .Net Client application to connect to a Web Service hosted on my own IIS over HTTP channel. I have done the fiddler debugging earlier with a Website hosted on my IIS and watching the traffic generated between my browser and the WebSite. But when it comes to watching the traffic generated by a .Net client program talking to the web service using HttpClient class, strangely the fiddler does not seem to be able to tap that traffic and does not show anything. Is .Net HttpClient bypassing the WinInet API to connect to the service which results in the fiddler not able to watch the traffic?

Ginseng answered 19/3, 2014 at 8:39 Comment(1)
.NET doesn't use WinINET, and Fiddler doesn't care. .NET does bypass proxies for localhost, a factor I mention in my answer.Jarl
J
30

Generally speaking, simply starting Fiddler before your application is sufficient. You haven't explained what you've tried so far.

Jarl answered 19/3, 2014 at 15:56 Comment(5)
I hope this will help to get an idea what I have tried so far. I have already tried using fiddler but it does not capture any traffic generated from my .net console application that is using HTTPClient class to reach out to the service hosted on the server on a custom port. – dhruvin just nowGinseng
You need to read and follow the instructions I've provided, or provide much more detail about the issue and what you've specifically tried. Thousands of developers successfully capture traffic in the scenario you describe every day.Jarl
Hi @Eric, I have updated my question to be more specific in the scenario I am having trouble with. Hope that helps clarify the question now.Ginseng
You still haven't provided a URL, but using my psychic debugging powers, I can tell you that you need to read this: fiddlerbook.com/fiddler/help/hookup.asp#Q-LocalTraffic to resolve your problem.Jarl
It's also worth checking your filters in fiddler. I wasted too long messing with this before realising my mistake.Kneepad
P
53

If you are connecting with a url like http://localhost:1234 change it to http://localhost.fiddler:1234/ and the requests from HttpClient should then become visible in Fiddler.

Portamento answered 30/6, 2015 at 10:50 Comment(1)
And the reason seem to be explained here - Microsoft inserted in their code a check and if it's "localhost" they simply skip the proxy so requests doesn't go through fiddler.Llama
J
30

Generally speaking, simply starting Fiddler before your application is sufficient. You haven't explained what you've tried so far.

Jarl answered 19/3, 2014 at 15:56 Comment(5)
I hope this will help to get an idea what I have tried so far. I have already tried using fiddler but it does not capture any traffic generated from my .net console application that is using HTTPClient class to reach out to the service hosted on the server on a custom port. – dhruvin just nowGinseng
You need to read and follow the instructions I've provided, or provide much more detail about the issue and what you've specifically tried. Thousands of developers successfully capture traffic in the scenario you describe every day.Jarl
Hi @Eric, I have updated my question to be more specific in the scenario I am having trouble with. Hope that helps clarify the question now.Ginseng
You still haven't provided a URL, but using my psychic debugging powers, I can tell you that you need to read this: fiddlerbook.com/fiddler/help/hookup.asp#Q-LocalTraffic to resolve your problem.Jarl
It's also worth checking your filters in fiddler. I wasted too long messing with this before realising my mistake.Kneepad
T
16

IIS does not use the proxy setting in Internet Option because it runs under a different user identity (default is ApplicationPoolIdentity). @EricLaw has provided a good pointer regarding the problem of capturing traffic of IIS/ASP.NET.

Instead of configuring IIS to use my login account, I edit web.config to force HTTPClient to use proxy, like following.

<configuration>
  <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
      <proxy proxyaddress="http://127.0.0.1:8888"/>
    </defaultProxy>
  </system.net>
</configuration>

Here is the detail of usage from MSDN.

Trici answered 21/4, 2015 at 21:31 Comment(0)
D
4

If the .NET application is running in your current user account, add the following content inside the configuration section:

<configuration>
 <system.net>
  <defaultProxy>
   <proxy bypassonlocal="false" usesystemdefault="true" />
  </defaultProxy>
 </system.net>
</configuration>

Note: Important: Regardless of other settings, .NET will always bypass the Fiddler proxy for URLs containing localhost. So, rather than using localhost, change your code to refer to the machine name. For instance:

This URL will not appear in Fiddler:

http://localhost/X509SignCodeService/X509SigningService.asmx

This URL will appear in Fiddler:

http://mymachine/X509SignCodeService/X509SigningService.asmx

Doxy answered 13/7, 2015 at 11:58 Comment(0)
G
2

For those who have the same problem with a .NET Core app and use Fiddler (should work for other tools too, but haven't checked this).

You need to know the port Fiddler listens to: Fiddler Everywhere Settings Modal

Then run (as Administrator on Windows):

netsh winhttp set proxy 127.0.0.1:<the port>

To remove the proxy, run:

netsh winhttp reset proxy

The source of wisdom you can find here. Works with localhost.

Glider answered 11/6, 2021 at 10:14 Comment(0)
R
0

Use HttpTracer - a library built to capture Http requests/responses. You won't need to configure a proxy, it's as easy as passing an instance of the handler into HttpClient: new HttpClient(new HttpTracerHandler()). Try it out, it's been invaluable for us on the Xamarin side and for S2S purposes in ASP.NET.

Reticle answered 18/7, 2019 at 22:36 Comment(1)
by "built specifically for this purpose", I think it should be made clear that it wasn't built specifically to resolve the Fiddler issue, but to provide an alternative means of tracing the calls and exposing the HTTP metadata to a developerWrongdoer
M
-1

all request to IIS are logged in logging directory (iis manager > iis server > logging) default is: %SystemDrive%\inetpub\logs\LogFiles

At the end of line there is status of the request.

Murrhine answered 25/4, 2014 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.