Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Debugging HttpClient Calls with fiddler in PCL

$
0
0

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


Viewing all articles
Browse latest Browse all 77050

Trending Articles