1. 引用FiddlerCore3dot5.dll 没找到可以q:974288432.

2.游览器用的cefsharp nuget导入的,没找到直接导入dll,nuget下了500M 

 if (!Cef.IsInitialized)
            {
                CefSettings setting = new CefSettings();
                //setting.CachePath = "Cache";
                setting.IgnoreCertificateErrors = true;
                setting.CefCommandLineArgs.Add("--ignore-urlfetcher-cert-requests", "1");//证书
                setting.CefCommandLineArgs.Add("--ignore-certificate-errors", "1");//不设置这两句我的提示错误:没证书
                //setting.CefCommandLineArgs.Add("--proxy-server", "http://127.0.0.1:9999");//游览器不用设置也会被fiddler抓包,大概是fiddler 初始化设置的是win10系统设置的代理,然后cefsharp不用设置就被代理了
                Cef.Initialize(setting);
            }

3.fidderHelp.cs

class Fiddler
    {
        static FieldInfo BeforeRequest;
        static FieldInfo BeforeResponse;
        public static void SetListenEvent(SessionStateHandler request, SessionStateHandler response)
        {
            if (BeforeRequest == null)
            {
                var fields = typeof(FiddlerApplication).GetFields(BindingFlags.NonPublic | BindingFlags.Static);
                BeforeRequest = fields[5];
                BeforeResponse = fields[6];
            }
            BeforeRequest.SetValue(null, null);
            BeforeResponse.SetValue(null, null);
            if (request != null)
                FiddlerApplication.BeforeRequest += request;
            if (response != null)
                FiddlerApplication.BeforeResponse += response;
        }
        public static void SetListenEventAndStart(SessionStateHandler request, SessionStateHandler response)
        {
            SetListenEvent(request, response);
            if (FiddlerApplication.IsStarted())
                FiddlerApplication.Shutdown();
            FiddlerApplication.Startup(9517, true, true, false);
        }
        public static void Exit()
        {
            if (FiddlerApplication.IsStarted())
            {
                FiddlerApplication.Shutdown();
            }
        }
}

4.找个地方 启动捕捉

 Fiddler.SetListenEventAndStart((oReq) =>
            {
                if (oReq.url.StartsWith(Entity.SearchPro))
                {
                    IsStart = true;
                }
            }, (oRes) =>
            {
                Console.WriteLine(oRes.url);
                if (oRes.url.StartsWith(Entity.SearchPro))
                {
                    string res = Encoding.Default.GetString(oRes.ResponseBody);

                    //var tem = EntryEngine.Serialize.JsonReader.Deserialize<SearchProData>(res);
                    //var tem = JsonConvert.DeserializeObject<SearchProData>(res);
                    //Stream myResponseStream = oRes.GetResponseStream();
                    //StreamReader myStreamReader = new StreamReader(res, Encoding.UTF8);
                    //string retString = myStreamReader.ReadToEnd();
                    Console.WriteLine($"res:{oRes.url}");
                    Console.WriteLine(oRes.GetResponseBodyAsString());
                }
            });

5.然后这里遇到问题: 

    服务器返回一个json包。我一开始用的是 oRes.ResponseBody(byte[]) ,json 类 解码参数是 string ,

我用byte[]转string Encoding.Default.GetString(oRes.ResponseBody) 换了几种编码全部是乱码,
然后发现有个
oRes.GetResponseBodyAsString()转出来就是编码正常的string 字符串{"server_time":1608429602,"error_code":40001}
后续看看 原因补充下
posted on 2020-12-20 10:33  九剑.sk  阅读(210)  评论(0编辑  收藏  举报