locked
How can I inspect my Xamarin.Forms app's network traffic? RRS feed

  • Question

  • User71550 posted

    I have an async method that calls a web service thus (I've omitted some boilerplate stuff):

    var client = new HttpClient(new NativeMessageHandler());
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
    var response = await client.SendAsync(httpRequest);
    var jsonString = await response.Content.ReadAsStringAsync();
    

    I know the client.SendAsync() call is successfully connecting to my service, but the requests aren't being picked up by Fiddler.

    I want to inspect the request and response headers but Fiddler isn't recording any traffic from my app. I'm using Genymotion emulation, and I've configured the emulator to use the Fiddler proxy (https://cloud.genymotion.com/page/faq/#android-proxy) successfully. If I use the emulator browser to browse the web, the requests appear in the traffic list, yet none of my app's traffic does.

    Does anyone have any insight regarding this?

    Thanks! Gabe

    Thursday, September 18, 2014 6:02 PM

All replies

  • User51765 posted

    Fiddler interferes with the simulator's network connection on our end. So what we do is we attach the iOS and Android device on the Mac and PC, run the app on the device, ( then for iOS, we disconnect the iphone from the Mac then attach it on the PC), then turn on fiddler. Make sure you follow Telerik's docs: (iOS)http://docs.telerik.com/fiddler/configure-fiddler/tasks/ConfigureForiOS , (Android) http://docs.telerik.com/fiddler/configure-fiddler/tasks/configureforandroid .

    Also, when you're done, set back your the WI-FI proxy on your device to 'Auto' or else your app or device won't connect to the internet when you disconnecte from the PC.

    Friday, September 19, 2014 8:13 AM
  • User71550 posted

    Thanks for replying JP, but what I'm looking for is a way to debug from visual studio and simultaneously view network traffic in fiddler. I've tried a lot of things, including trying to reconfigure the network settings in Virtual Box, but all I managed to do was make Genymotion unusable :(

    Monday, January 12, 2015 7:25 PM
  • User142812 posted

    Would love to know if someone found a solution to make Fiddler working on Windows.

    Tuesday, August 25, 2015 12:05 PM
  • User134292 posted

    Anyone tried monitoring on VisualStudio 2015?

    http://blogs.msdn.com/b/visualstudio/archive/2015/05/04/introducing-visual-studio-s-network-tool.aspx

    I'm still using the 2013...

    Tuesday, August 25, 2015 12:31 PM
  • User70014 posted

    Doesn't appear to be available in VS1015. I tried for iOS, Droid and WinPhone8.

    Tuesday, August 25, 2015 1:19 PM
  • User928 posted

    Hi, I might have a solution.

    So I had a massive struggle with this yesterday believing that it must be possible.

    So, how to do it?

    First you should do this: https://blogs.msdn.microsoft.com/visualstudioalm/2015/05/06/using-fiddler-to-monitor-network-traffic-from-the-vs-emulator-for-android/

    This is helpful to set up for the next step but unfortunately that won't let you see your apps traffic. The reason is that what you have done is set up a default proxy for the emulator but the app won't use it. That will be useful if you want to see other http traffic such as browser traffic from the emulator.

    Next you will have to create a proxy for the app to use. Usually it would look something like this for web requests:

    WebRequest.DefaultWebProxy = new WebProxy();
    

    or for httpclient

    var handler = new HttpClientHandler{Proxy = new WebProxy(uri)};
    

    Unfortunately some xamarin profiles won't have access to web proxy so we'll have to make our own. The most important thing is that we implement IWebProxy. You can use this:

    public class Proxy : System.Net.IWebProxy
        {
            public System.Net.ICredentials Credentials
            {
                get;
                set;
            }
    
            private readonly Uri _proxyUri;
    
            public Proxy(Uri proxyUri)
            {
                _proxyUri = proxyUri;
            }
    
            public Uri GetProxy(Uri destination)
            {
                return _proxyUri;
            }
    
            public bool IsBypassed(Uri host)
            {
                return false;
            }
        }
    

    Now that we have a proxy class we need a uri which we make using the ip and port from fiddler which we found in the above article:

    var uri = new Uri("http://{fiddlerIP}:{fiddlerPort}/");
    

    Then we can use this for WebRequest:

    WebRequest.DefaultWebProxy = new Proxy();
    

    or for HttpClient: var handler = new HttpClientHandler { Proxy = new Proxy(uri) } var httpClient = new HttpClient(handler);

    I hope this helps!

    PS. This editor is constantly suggesting smileys to me and I can't fix this last code block :neutral:

    Wednesday, June 15, 2016 11:24 PM
  • User31385 posted

    On iOS at least there's NSURLProtocol which can intercept network requests as they are being made. It's extremely powerful and really not hard to use.

    Of course this only works if you're using the NSURLConnection or NSURLSession classes on iOS, or if you've got ModernHttpClient or the latest shiny networking from Xamarin.

    I don't know if there's an equivalent on Android.

    Wednesday, June 15, 2016 11:58 PM