繁体   English   中英

无法让Fiddler使用本地主机不同的端口捕获来自MVC的HttpClient Web API调用

[英]Can't get Fiddler to capture HttpClient web api calls from MVC with localhost different port

另一个提琴手无法获取帖子。

与此SO Post类似,我花了两个小时阅读和尝试不同的解决方案,但没有一个使我看到我的提琴手Web api流量。

顺便说一句,我的代码正在工作,我只是专注于让提琴手向我展示api调用。

我将描述我的设置,然后再尝试。

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

HTTP://本地主机:63381 /

我的ASP.NET MVC 5 Web客户端项目在以下端口上运行:59722

HTTP://本地主机: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);
   }
}
  1. 将块添加到32和62 machine.config。 重新启动Visual Studio中没有重新启动计算机或任何其他服务。 这没有用。

  2. 将块添加到客户端web.config中,此操作不起作用。

  3. 将localhost更改为machinename:port具体来说,我将http:// localhost:63381 / api / MyApi更改http:// gpgvm-pc:63381 / api / MyApi

  4. 使用以下命令修改了Global.asax:

    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate {return true;});

  5. 提琴手自定义规则

  6. 反向代理

  7. 设置提琴手在另一个端口上侦听。

至此我投降了。 在我看来,#1应该可以捕获所有内容,但是显然我仍然在做错什么,因为我可以让提琴手捕获一个或另一个,而不是客户端取消向客户端的请求???


更新:

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

问题很可能是您使用以特殊方式处理的本地主机。

尝试使用计算机名称或您的IP(不要使用127.0.0.1)。

该文档也包含有关此信息:

http://docs.telerik.com/fiddler/Observe-Traffic/Troubleshooting/NoTrafficToLocalhost

如果您尝试在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" });
            }
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM