为什么在 ASP.NET 中调用 API 只有在我打开 Fiddler 时才有效

本文关键字:Fiddler 有效 NET ASP 调用 为什么 API | 更新日期: 2023-09-27 18:36:37

我像这样在我的索引控制器中调用一个 API,并且一切正常,只有当我打开 Fiddler 时。

public ActionResult Index()
{
    Base model = null;
    var client = new HttpClient();
    var task =
        client.GetAsync(
        "http://example.api.com/users/john/profile")
        .ContinueWith((taskwithresponse) =>
        {
            var response = taskwithresponse.Result;
            var readtask = response.Content.ReadAsAsync<Base>();
            readtask.Wait();
            model = readtask.Result;
        });
    task.Wait();
    return View(model);
}

如果我关闭小提琴手,我会收到以下错误:

无法建立连接,因为目标计算机处于活动状态 拒绝了 127.0.0.1:8888

我是否必须包含一些配置,以便调用 API 有效,即使我没有打开 Fiddler。

为什么在 ASP.NET 中调用 API 只有在我打开 Fiddler 时才有效

检查 web.config 是否有任何代理配置,并检查是否已配置了 .net 可能使用的系统默认代理。您可以将其添加到 web.config 以禁用代理配置:

    <system.net>
      <!-- set enabled to true to use the system default proxy -->
      <defaultProxy enabled="false" />
    </system.net>