如何在HttpClient中使用Fiddler? [英] How to use Fiddler with HttpClient?

查看:208
本文介绍了如何在HttpClient中使用Fiddler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多问题/答案,关于此的博客,而不是在谈论Telerik的常见问题解答。我仍然找不到以明确的纯方法诊断和解决的问题:

I know there are many of questions/answers, blogs about this, not talking about Telerik's FAQ. Still I could not find this diagnosed and solved in a clear pure way:

上下文:

我有一个Web API应用程序,并且有一个(单元测试)客户端,该客户端使用HttpClient向API发送请求。 Web API应用程序监听 http:// localhost:8631 /
有时我使用Fiddler来查看发生了什么。

I have a Web API app, and I have a (unit test) client, which uses HttpClient to send requests to the API. Web API app listens in http://localhost:8631/ Sometimes I use Fiddler to see what's going on.

问题:

Fiddler无法捕获HttpClient和Web API之间的流量。启动Fiddler之后,流量仍然可以,但未在Fiddler中显示。

Traffic between my HttpClient and Web API is not captured by Fiddler. After launching Fiddler traffic is still OK, but not shown in Fiddler.

到目前为止的诊断:

  • Important: Using any browser and sending requests to http://localhost:8631/ is working, I mean: traffic captured by Fiddler
  • Configuring HttpClient explicitly to use Fiddler as proxy does not help.
  • Changing HttpClient url from http://localhost:8631/ to http://localhost.fiddler:8631/ helps, regardless of proxy was configured or not.

结论:
至少我的情况:HttpClient是否已明确配置为使用Fiddler作为代理。这与HttpClient和/或Fiddler的localhost行为有关。

Conclusions: At least my case: It is not about HttpClient is configured explicitly to using Fiddler as proxy or not. It is about HttpClient's and/or Fiddler's localhost behaviour.

再次发出:

一个人可能会问:问题解决了,那么问题是什么?嗯...

One may ask: Problem solved, then what is the question? Well...

第一季度:这仍然是一个痛苦的问题,因为URL是在某个地方编码或配置的(我的意思是 http:// localhost:8631 / http:// localhost .fiddler :8631,因此必须更新fiddler的每次启动和停止。更多:将源代码从源代码管理中检出,并由队友在另一台计算机上检出,可能会引起问题。 :对此有没有更痛苦的解决方法?

Q1: This is still a painful issue, because the url is coded or configured somewhere (I mean http://localhost:8631/ or http://localhost.fiddler:8631 so every start and stop of fiddler it must be updated. More: Checking in the source to source control, and checking out on an other machine by a teammate may cause issue. So: Is there any less painful workaround for this?

硬编码我的机器名称(这也可能起作用)在团队工作和使用时会引起同样的痛苦和问题源代码控制

Hard coding my machine name (which also could work) causes the very same pain and issue when working in a team and using source control

第二季度:为什么这种不一致的行为:纯 http: // localhost:8631 / 可以在任何浏览器中使用,但不能在HttpClient中使用。

Q2: Why is this inconsistent behaviour: Pure http://localhost:8631/ works from any browser but not from HttpClient.

我认为回答第二季度可以使我们更接近一种更有用的解决方法。

I think answering Q2 can get us closer to a more usable workaround.

代码展示

    // Using the following url  w o r k s  regardless of any proxy setting
    // ...but it is a pain to hardcode or configure this and change    depending on Fiddler is running or not

    //private const string ApiUrl = "http://localhost.fiddler:8631/"; 

    // This is not working regardless any proxy setting. Trafic bypasses Fiddler
    private const string ApiUrl = "http://localhost:8631/";

    protected HttpClient GetClient()
    {
        var httpClientHandler = new HttpClientHandler
        {
            // Does not work 
            //Proxy = new WebProxy("http://localhost:8888", false),

            // Does not work
            Proxy = WebRequest.DefaultWebProxy,
            UseProxy = true
        };


        var client = new HttpClient(httpClientHandler)
        {
            BaseAddress = new Uri(ApiUrl)
        };
        // ...


推荐答案

问题是 WebProxy 类的Microsoft实现具有对回送URL的静态检查(基于名称列表,如 localhost),并且将绕过标识为URI的任何代理回送。即使 BypassProxyOnLocal 设置也没有关系。仅当您在本地网络中使用本地计算机名称或其他计算机名称时,此设置才有效。主机localhost或ip地址127.0.0.1始终被识别为环回,并会导致绕过代理。

The problem is that the Microsoft implementation of the WebProxy class has a static check for loopback urls (based on a list of names like "localhost") and will bypass any proxy for uris identified as loopback. Even the BypassProxyOnLocal setting will not matter. This setting only has an effect if you use the local machine name or another machine name in the local network. The host localhost or the ip address 127.0.0.1 are always recogized as loopback and will lead to bypassing the proxy.

.net框架代码的相关部分位于 WebProxy.IsBypassedManual

Relevant part of the .net framework code is in WebProxy.IsBypassedManual:

if (host.IsLoopback) {
    return true; // bypass localhost from using a proxy.
}

编写您自己的WebProxy类的后代并覆盖 GetProxy IsBypassed 方法可使用代理返回uri,即使是回送URL。然后将该类的实例分配给用于创建 HttpClient HttpClientHandler

Write your own descendant of the WebProxy class and overwrite the GetProxy and IsBypassed methods to return an uri using the proxy even for loopback urls. Then assign an instance of that class to the HttpClientHandler you use to create the HttpClient.

似乎不起作用,因为.net代码希望与实现IAutoWebProxy的对象一起使用,但是IAutoWebProxy被声明为内部对象,不能在我们自己的代码中使用。

Doesn't seem to work because the .net code expects to work with objects that implement IAutoWebProxy, but IAutoWebProxy is declared internal and cannot be used in our own code.

我看到的最简单的解决方案是在运行时使用ApiUrl中的本地计算机名称替换 localhost的功能。无论Fiddler是否正在运行,本地计算机名称都将起作用。

The easiest solution I see is to have a feature that replaces "localhost" with the local machine name in ApiUrl at runtime. The local machine name will work regardless of whether Fiddler is running or not.

这篇关于如何在HttpClient中使用Fiddler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆