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

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

问题描述

我是HttpClient类的新手,我遇到使用 Charles Proxy 监控请求的问题。基本上我需要的是监视从模拟器或实际iOS设备发出的请求。 这里你可以找到一个很好的教程如何配置Charles for iOS开发。
我正在制作简单的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.

我想也许,这是因为我正在使用模拟器,但事实并非如此。我尝试打开野生动物园并浏览了一些网页,并立即出现了流量。所以问题不在于模拟器。

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类,我不知道为什么尽管它正常工作,那就是发送/收到请求后,Charles无法捕获此类请求。

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.

全部谢谢你的回复

亲切的问候Gagik

Kind Regards Gagik

推荐答案

有很多方法可以初始化 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天全站免登陆