I am new to the HttpClient class and I have a problem with monitoring requests using Charles Proxy . Basically, I need to track requests that are made either from the simulator, or from the actual iOS device. Here you can find a great tutorial on how to set up Charles for iOS development. I made simple HttpClient requests, for example, just authorization
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 authorized, and the carrier token is returned. But what was the problem that my simulator requests did not appear on the http traffic monitoring list:
I thought maybe it was because I was using a simulator, but it wasn’t. I tried to open the safari and looked at the web page, and traffic immediately appeared. So the problem is not in the simulator.
I also tried installing on the device, and again the same story, when using the HttpClient the traffic monitoring screen remains quiet, but as soon as I open the browser, the traffic screen starts sorting and usurping all requests.
I thought this was because I use HTTPS, although in any case, at least the request header should be captured, even if the body is encoded. But this was not so, I tried to open some HTTPS site on my safari on the device, and again traffic appeared on the screen of my Charles.
The next thing I did, I downloaded the monotouch HttpClient sample. And the good news is that there are several ways to send requests, in fact four of them are 1. http WebRequest, 2. https WebRequest, 3. http NSUrlConnection, 4. HttpClient.
And I tried all of them, since you can guess that the first three appeared perfectly in charles, but the last HttpClient again I do not know why it did not appear on the traffic log screen.
So, I am 100% sure that the problem is the HttpClient class, which I do not know why, despite the fact that it works fine, these are send / receive requests, requests made by this class cannot be captured by Charles.
And to rule out the last possible cause of this problem, it could be a problem in Charles, I also tried using Fiddler on Windows, which worked like a virtual machine on my Mac ( here you can find how to do it), the same story repeated - all requests made by HttpClient were not captured, the rest (WebRequests, NSUrlConnection-s, Safari open web pages) worked fine,
Please, can someone suggest me if this is some kind of mistake, maybe there is a workaround or another solution to this problem.
Thank you all for your answers.
Regards Gagik