我已经按照Fiddler网站这里所述的每一步操作,以捕获我的应用程序(运行在模拟器中)的网络数据包。奇怪的是,我可以解码从浏览器发送的http / https,但无法解码来自我的应用程序的https数据包。更有趣的是,请求能够成功响应!有什么想法可能是问题所在吗?
我完全困惑了,不知道该做什么。
我完全困惑了,不知道该做什么。
我在Xamarin论坛上找到的第一个话题是为什么Fiddler不能检测到来自Xamarin应用程序的流量?。答案是设置DefaultWebProxy
:
public override void OnCreate()
{
// get IP from fiddler's internet icon at the top right, or via ipconfig for VirtualBox used by Genymotion
WebRequest.DefaultWebProxy = new WebProxy("192.168.56.1", 8888);
base.OnCreate();
...
}
我把它放在MainActivity
中,但这并没有解决我的问题。
对我有帮助的是 使用fiddler调试PCL中的HttpClient调用:
你需要改变你的代码来使用一个HttpClientHandler和代理
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
}
看起来很简单,但是由于某些原因,PCLs在其捆绑的System.Net dll中不包括Web代理类,因此您需要通过扩展System.Net.IWebProxy接口来编写自己的代理类。
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;
}
}
经过这一系列操作后,我能够在Android模拟器上捕获我的Xamarin.Forms应用程序的请求。