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

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

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

Configure a .NET application to use Fiddler?

If you're coding a .NET application, K Scott Allen's blog shows a simple way to hook Fiddler temporarily for debugging purposes:

GlobalProxySelection.Select = new WebProxy("127.0.0.1", 8888);

Note that you might not even need to do this-- The Framework should autodetect the WinINET proxy when the .NET application starts.  Note that this means that Fiddler must be started BEFORE your application if your application is to autodetect Fiddler.

You may specify a proxy inside the yourappname.exe.config file. If the .NET application is running in your current user account, you can simply add the following content inside the configuration section:

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

See http://msdn.microsoft.com/en-us/magazine/cc300743.aspx for more on this topic.

If you need to get code running in a different user-account (e.g. a Windows Service) to send traffic to Fiddler, you will need to edit the configuration inside the machine.config.

<!-- The following section is to force use of Fiddler for all applications, including those running in service accounts -->  <system.net>
  <defaultProxy>
    <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
  </defaultProxy>
</system.net>

If all else fails, you can manually specify the proxy on an individual WebRequest object, like so:

objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Proxy= new WebProxy("127.0.0.1", 8888);

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:

Does not show in Fiddler: http://localhost/X509SignCodeService/X509SigningService.asmx

Shows in Fiddler: http://mymachine/X509SignCodeService/X509SigningService.asmx

Using a proxy with .NET 4.5 HttpClient

This code worked for me:

var httpClientHandler = new HttpClientHandler
                        {
                            Proxy = new WebProxy("http://localhost:8888", false),
                            UseProxy = true
                        }

Note that I am not supplying an empty array to my WebProxy constructor. Perhaps that's the problem?

原文地址:https://www.cnblogs.com/chucklu/p/15219244.html