似乎无法将Fiddler代理设置为HttpClient

3

我一直在尝试让Fiddler捕获自托管ASP.NET Web API的流量,但遇到了问题。我的情况可能有点不同,因为托管Web API的应用程序也在使用它(是的,我们有理由这样做 :))。我们正在使用HttpClient。这里是我尝试过的事情:

  1. Adding an HttpClientHandler and setting up the proxy.
  2. added the following to app.config

    <system.net>
    <defaultProxy>
      <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost:8888"/>
    </defaultProxy>
    

  3. Tried to hit the api from a separate .net client client (thinking the same process consuming the api that is hosting it was not allowing the traffic to be captured since it was all Intra-process).

现在,如果我在本地打开Web浏览器或Postman并访问该API,则可以正确捕获流量。
以下是客户端代码:
    var handler = new HttpClientHandler();
    handler.UseProxy = true;
    handler.Proxy = new WebProxy("http://127.0.0.1",8888);

    var client = new HttpClient(handler) {BaseAddress = new Uri(new Uri("http://localhost:8085"), "api/companies")};

    HttpResponseMessage response;
    using (client)
    {
        response = client.GetAsync(client.BaseAddress).Result;
    }
    var result = response.Content.ReadAsAsync<IEnumerable<Company>>().Result;

理想情况下,我希望能够在运行应用程序时仅打开 Fiddler 并捕获流量(即使在生产环境中),而无需更改源代码。

1个回答

5

谢谢,好使!!!省去了我很多不必要的时间。我还为您创建的用户反馈投了赞成票。 - coding4fun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接