使用 MonoTouch、HttpClient 和 Charles Proxy 时的 HTTP 流量监控问题 [英] HTTP Traffic monitoring issue when using MonoTouch, HttpClient and Charles Proxy

查看:36
本文介绍了使用 MonoTouch、HttpClient 和 Charles Proxy 时的 HTTP 流量监控问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 HttpClient 类的新手,我在使用 Charles 代理 监控请求时遇到了问题.基本上我需要的是监控来自模拟器或实际 iOS 设备的请求.在这里你可以找到一个很好的教程为 iOS 开发配置 Charles.例如,我正在发出简单的 HttpClient 请求,只是一个简单的授权

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;
        }

代码有效,用户被授权,并且不记名令牌被返回.但问题是,我在模拟器上的请求没有出现在 Charles http 流量监控列表中.

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.

我想也许是因为我在使用模拟器,但事实并非如此.我尝试打开 safari 并浏览了一些网页,流量立即出现.所以问题不是来自模拟器.

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.

我也尝试在设备上安装,同样的故事,当使用 HttpClient 时,流量监控屏幕保持沉默,但是一旦我打开浏览器,流量屏幕就会开始抖动并篡夺所有请求.

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.

我想可能是因为我使用 HTTPS,尽管无论如何至少应该捕获请求标头,即使主体已编码.但事实并非如此,我尝试在我的设备 safari 上打开一些 HTTPS 站点,但流量再次出现在我的 Charles 屏幕上.

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.

接下来我下载了 monotouch HttpClient 示例.好消息是,有多种发送请求的方法,实际上有四种——1. http WebRequest,2. https WebRequest,3. http NSUrlConnection ,4. HttpClient.

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.

我都试过了,你可能猜到前三个完美地出现在查尔斯身上,但最后一个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.

所以我 100% 确定问题是 HttpClient 类,我不知道为什么尽管它正常工作,即发送/接收请求,但此类发出的请求不能被查尔斯抓获.

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.

为了排除这个问题的最后一个可能的原因,也就是问题出在 Charles 身上,我还尝试在 Windows 上使用 Fiddler,它在我的 Mac 上作为虚拟机运行(这里 你可以找到如何做到这一点),同样的故事被重复了——所有的请求都是由HttpClient 没有被捕获,其余的(WebRequests、NSUrlConnection-s、safari 网页打开)工作得很好.

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.

感谢大家的回复

亲切的问候加吉克

推荐答案

有很多方法可以初始化 HttpClient.某些方式不会与操作系统对话(完全托管),也不会知道 iOS 代理设置.

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.

最好的(对于 iOS)通常是使用使用 CFNetwork 的处理程序,参见 此博客 了解更多详情.基本上它的意思是:

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 ());

否则您需要将 HttpClientHandler.Proxy 设置为 CFNetwork.GetDefaultProxy.例如

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);

这篇关于使用 MonoTouch、HttpClient 和 Charles Proxy 时的 HTTP 流量监控问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆