简体   繁体   中英

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

Yet another fiddler can't get it to capture post.

Similar to this SO Post I have spent two hours now reading and trying different solution yet none of them allow me to see my fiddler web api traffic.

As a side note my code is working I am just focused on getting fiddler to show me the api calls.

I will describe my setup then what I have tried.

My Web API is a separate MVC 6, EF 7 project running on port 63381

http://localhost:63381/

My ASP.NET MVC 5 web client project is running on port: 59722

http://localhost:59722/

A typical action controller in the mvc client:

//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. Added the block to 32 & 62 machine.config. Restarted visual studio did NOT restart machine or any other service. This did not work.

  2. Added the block to the client web.config and this didn't work.

  3. Changed localhost to machinename:port Specifically I changed http://localhost:63381/api/MyApi to http://gpgvm-pc:63381/api/MyApi

  4. Modified Global.asax with:

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

  5. Fiddler custom rules

  6. Reverse proxy

  7. Set fiddler listening on a different port.

At this point I surrender. It seems to me #1 should work to capture everything but I am obviously still doing something wrong because I can get fiddler to capture one or the other but NOT the client calling off to the client???


Update:

My machine locked and after reboot I started seeing the api calls so this issue was something with my machine. So sorry to bother.

The problem is most likely that you are using localhost which are handled in a special way.

Try using machine name or your ip instead (do not use 127.0.0.1).

The documentation have information about this as well:

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

If you try to hit specific action in api then use that code in webapi config

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 });

        }

This code where u call your 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" });
            }
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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