C# Can';不要让Fiddler使用localhost不同的端口从MVC捕获HttpClient web api调用

C# Can';不要让Fiddler使用localhost不同的端口从MVC捕获HttpClient web api调用,c#,asp.net,fiddler,C#,Asp.net,Fiddler,然而,另一个小提琴手却没能把它抓住 与此类似,我现在花了两个小时阅读和尝试不同的解决方案,但没有一个允许我看到我的fiddler web api流量 顺便说一句,我的代码正在运行,我只是专注于让fiddler向我展示api调用 我将描述我的设置,然后我尝试了什么 我的Web API是一个独立的MVC 6、EF 7项目,运行在端口63381上 我的ASP.NET MVC 5 web客户端项目正在端口59722上运行 mvc客户端中的典型操作控制器: //Controller CTOR publ

然而,另一个小提琴手却没能把它抓住

与此类似,我现在花了两个小时阅读和尝试不同的解决方案,但没有一个允许我看到我的fiddler web api流量

顺便说一句,我的代码正在运行,我只是专注于让fiddler向我展示api调用

我将描述我的设置,然后我尝试了什么

我的Web API是一个独立的MVC 6、EF 7项目,运行在端口63381上

我的ASP.NET MVC 5 web客户端项目正在端口59722上运行

mvc客户端中的典型操作控制器:

//Controller CTOR
public ClientController()
{
  client = new HttpClient();
  client.BaseAddress = new Uri("http://localhost:63381/api/MyApi");
  client.DefaultRequestHeaders.Accept.Clear();
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

//Action within ClientController
public async Task<JsonResult> AddNewSubCategory()
{
   HttpResponseMessage responseMessage = await client.PostAsJsonAsync(url2, content);
   if (responseMessage.IsSuccessStatusCode)
   {
     return Json("[{Update Success}]", JsonRequestBehavior.AllowGet);
   }
     return Json("[{Error Updating}]", JsonRequestBehavior.AllowGet);
   }
}
//控制器
公共客户端控制器()
{
client=新的HttpClient();
client.BaseAddress=新Uri(“http://localhost:63381/api/MyApi");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
}
//ClientController中的操作
公共异步任务AddNewSubCategory()
{
HttpResponseMessage responseMessage=Wait client.PostAsJsonAsync(url2,内容);
if(responseMessage.IsSuccessStatusCode)
{
返回Json(“[{Update Success}]”,JsonRequestBehavior.AllowGet);
}
返回Json(“[{Error updatening}]”,JsonRequestBehavior.AllowGet);
}
}
  • 将块添加到32&62 machine.config。重新启动的visual studio未重新启动计算机或任何其他服务。这不起作用

  • 将块添加到client web.config,但此操作无效

  • 已将localhost更改为machinename:端口 具体来说,我改为

  • 使用以下内容修改了Global.asax:

    ServicePointManager.ServerCertificateValidationCallback=新的RemoteCertificateValidationCallback(委托{return true;})

  • 小提琴手习惯规则

  • 反向代理

  • 设置fiddler在不同端口上侦听

  • 在这一点上,我投降了。在我看来#1应该努力捕捉所有东西,但我显然仍在做错事,因为我可以让fiddler捕捉其中一个,但不能让客户向客户叫停


    更新:


    我的机器被锁定,重新启动后,我开始看到api调用,所以这个问题与我的机器有关。很抱歉打扰您。

    问题很可能是您使用的是以特殊方式处理的localhost

    尝试改用计算机名或ip(不要使用127.0.0.1)

    文档中也有关于这方面的信息:


    如果您试图在api中点击特定操作,请在webapi配置中使用该代码

    public static void Register(HttpConfiguration config)
    
            {
                //config.Routes.MapHttpRoute(
                //    name: "DefaultApi",
                //    routeTemplate: "api/{controller}/{id}",
                //    defaults: new { id = RouteParameter.Optional });
    
               config.Routes.MapHttpRoute("API Default", "api/{controller}/{action}/{id}",
               new { id = RouteParameter.Optional });
    
            }
    
    这段代码是您调用api的地方

     public ActionResult ClientController(model content)
    
            {
                try
                {
                    HttpClient client = new HttpClient("http://localhost:63381/");
                    client.BaseAddress = new Uri();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                    HttpResponseMessage response = client.PostAsJsonAsync("api/MyApi/url2", content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        return Json(new { code = 0, message = "Success" });
                    }
                    else
                    {
                        return Json(new { code = -1, message = "Failed" });
                    }
                }
                catch (Exception ex)
                {
                    int code = -2;
                    return Json(new { code = code, message = "Failed" });
                }
            }
    }
    

    有在Fiddler中设置的过滤器吗?没有。抱歉,没有提到我签入了OP。我尝试了机器名称…请参阅OP中的第3点。现在可能是我没有正确执行。我将用我尝试过的方法更新OP。当我尝试从mvc客户端调用api时,HttpClient给我的主机名无效。请尝试改为使用您的ip。