Debugging HttpClient Calls with fiddler in PCL

stephenshaw.3366stephenshaw.3366 USMember ✭✭
edited December 2015 in Xamarin.Forms

Hello

I noticed there were very few guides on how to capture requests from your phone through fiddler while debugging so though i'd note down how to go about it in a pcl project

This is aimed at android devices but should be a similar enough setup for ios

First you will need to set fiddler up to capture packets from remote devices

Tools->Fiddler Options->Connections

  • Make sure "Allow remote computers to connect" is ticked
  • Make a note of the port number fiddler listens on for later

Tools->Fiddler Options->HTTPS

  • Make sure "Capture HTTPS CONNECTs" and "Decrypt HTTPS traffic" are checked

Click "Ok"

Open a cmd prompt and run "ipconfig" to get your ip address, make a note of this for later

Next your on phone long press the wifi connection you want to use and modify the network config

  • Tick "Show advanced options"
  • Under "Proxy" select Manual
  • Under "Proxy hostname" enter the ip address of your computer from the ipconfig
  • Under "Proxy port" enter the port fiddler is running under from the fiddler options connection tab

Connect to this network

HTTPS Connections

If you are going to be calling HTTPS you are going to need to install the Fiddler certificate

  • Open a browser on your phone and go to the ipaddress and port number you entered in the network config of you connected network
  • Click the link for "Fiddler Root Certificate" and install the link

You will now be able to see traffic coming in from your phone on fiddler but if your app is calling an api using the httpclient class then there is another bit of work you will need to do.

HttpClient

You will need to change your code to use a HttpClientHandler and a proxy

The following is an example of what that looks like

HttpClientHandler handler = new HttpClientHandler () {
    Proxy = new WebProxy (new Uri("http://[Computer IP]:[Fiddler port number]")),
    UseProxy = true
};
 using (var client = new HttpClient(handler))
{
    // Make your call in here
}

Looks simple enough only PCLs dont include the Web proxy class in its bundled System.Net dll for whatever reason so you will need to write your own by extending the System.Net.IWebProxy interface
like so

public class WebProxy : System.Net.IWebProxy
{
    public System.Net.ICredentials Credentials
    {
        get;
        set;
    }

    private readonly Uri _proxyUri;

    public WebProxy(Uri proxyUri)
    {
        _proxyUri = proxyUri;
    }

    public Uri GetProxy(Uri destination)
    {
        return _proxyUri;
    }

    public bool IsBypassed(Uri host)
    {
        return false;
    }
}

After all this faffing about you will then be able to see the calls coming through fiddler

Posts

  • BobFuryBobFury USMember

    Just wanted to say thank you for this. I had been hunting high and low trying to figure out how to see the traffic via Fiddler. This worked like a charm!

  • Phil.JSPhil.JS GBUniversity

    Great post, a gotcha for me was restarting fiddler after changing settings

  • gvsharmagorintagvsharmagorinta USMember ✭✭

    How to create a WebProxy class in PCL? In my case it's clearly raising compile time error. can you brief this?

  • MarkDownesMarkDownes USUniversity ✭✭

    After banging my head against the wall for many hours I stumbled upon this suggestion. You saved me - thanks!

  • ChristianSvrdChristianSvrd SEMember ✭✭✭

    why doesn't HttpClient respect the Android's Proxy setting?

Sign In or Register to comment.