Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Traffic monitoring issue when using MonoTouch, HttpClient and Charles Proxy

I'm new to HttpClient class and I'm having issue with monitoring requests using Charles Proxy. Basically what I need is to monitor the requests which are made either from simulator or actual iOS device. Here you can find a great tutorial of how to configure Charles for iOS development. I was making simple HttpClient requests for instance, just a simple authorisation

async Task<string>  authorizeUser()
        {
            HttpClient _client = new HttpClient ();
            _client.BaseAddress = new Uri("https://...../api/");
            _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue ("bearer", token);
            var content = new FormUrlEncodedContent(new[] 
                {
                    new KeyValuePair<string, string>("grant_type", "password"),
                    new KeyValuePair<string, string>("username", "theUserName"),
                    new KeyValuePair<string, string>("password", "thePassword")
                });
            var result = await _client.PostAsync("auth", content);
            string resultContent = result.Content.ReadAsStringAsync().Result;
            return resultContent;
        }

The code works, the user is being authorised, and the bearer token is being returned. But what was the issue, that my requests on the simulator were not appearing in the Charles http traffic monitoring list.

I thought perhaps, it is because I'm using simulator, but that was not the case. I tried opening the safari and browsed some web page and the traffic immediately appeared. So the issue was not from the simulator.

I also tried installing on the device, and again the same story, when using HttpClient the traffic monitoring screen remains silent, but as soon as I open the browser, the traffic screen starts jiggling and usurping all the requests.

I thought may be it is because I use HTTPS, although in any case at least the request header should be captured, even though the body is encoded. But that was not the case, I tried opening some HTTPS site on my device safari and again the traffic appeared on my Charles screen.

The next thing that I did I downloaded monotouch HttpClient sample. And the good news is that there are several methods of sending requests, actually four of them - 1. http WebRequest, 2. https WebRequest, 3. http NSUrlConnection , 4. HttpClient.

And I tried them all, as you may guess first three perfectly appeared in charles, but the last HttpClient again I don't know why didn't show up in traffic log screen.

So I'm 100% sure that the issue is the HttpClient class, which I don't know why despite the fact that it is working normally, that is sending/receiving requests, the requests made by this class can not be captured by Charles.

And to exclude the last possible reason for this issue, that is may be the problem is in Charles, I also tried using Fiddler on Windows, which was running as a virtual machine on my Mac (here you can find how to do that), the same story was repeated - all the requests made by HttpClient were not captured, the rest (WebRequests, NSUrlConnection-s, safari web page openings) worked perfectly fine.

Please, can anybody suggest me, whether it is some kind of bug, may be there is workaround or other solution to this problem.

Thanks all for your Replies

Kind Regards Gagik

like image 274
kyurkchyan Avatar asked May 06 '14 10:05

kyurkchyan


People also ask

How to use Charles proxy tool to monitor mobile traffic?

Enter the password. The certificate will be downloaded automatically. Give a proper name when prompted and then save. Setup is completed now and can monitor the traffic of your mobile in the Charles proxy tool. If you want to log the traffic only from mobile, then you can disable window proxying from the proxy tool.

What is Charles proxies?

Charles Proxy is exactly what its name implies: a proxy. This proxy is special, however, as it's specifically aimed at giving functionality that developers will need. It allows you to view, change, and replay the traffic that passes through, and can handle SSL.

How do I enable SSL proxying on Charles?

Select Proxy > SSL Proxying Settings > Verify you see this image Charles will now begin to capture the HTTPS traffic for the site URL. In order to capture other site URL, repeat steps 7, 8, 9 and ensure the URL is added to the SSL Proxying.

How to install Charles web debugging proxy on Mac OS X?

Step 1. Download Charles proxy and then install Charles - Download Charles Web Debugging Proxy Application Step 2. Open Charles Step 3. Navigate to Proxy > select MAC OS X Proxy Step 4. Navigate to Proxy > Proxy Settings > enable Use a dynamic port Step 5. Navigate to Help > SSL Proxying > Install Charles Root Certificate Step 6.


1 Answers

There's many ways to initialize HttpClient. Some ways won't talk with the OS (fully managed) and won't be aware of the iOS proxy settings.

The best (for iOS) is generally to use the handler that uses CFNetwork, see this blog for more details. Basically it means:

var client = new HttpClient (CFNetworkHandler ());

Otherwise you'll need to set the HttpClientHandler.Proxy to CFNetwork.GetDefaultProxy. E.g.

var handler = new HttpClientHandler {
    Proxy = CFNetwork.GetDefaultProxy (),
    UseProxy = true,
};
var client = new HttpClient(handler);
like image 105
poupou Avatar answered Sep 28 '22 00:09

poupou