Fiddler automatically saves the captured json data

1. Open the fiddler background script code

#输入指令,打开脚本代码
crtl+ r

2. Replace the OnBeforeResponse(oSession: Session) function (note that the json data is saved under the path E://spide//img, or you can define it yourself)

    static function OnBeforeResponse(oSession: Session) {
        if (m_Hide304s && oSession.responseCode == 304) {
            oSession["ui-hide"] = "true";
        }
        // if (oSession.fullUrl.Contains("baidu.com")){
        if (1){
            oSession.utilDecodeResponse();//消除保存的请求可能存在乱码的情况

            var jsonString = oSession.GetResponseBodyAsString();
            var responseJSON = Fiddler.WebFormats.JSON.JsonDecode(jsonString);
            if((responseJSON.JSONObject=='System.Collections.ArrayList' || responseJSON.JSONObject=='System.Collections.Hashtable')&&jsonString!='[]'&&jsonString!='{}'){
                // 判断是否是json数据 然后保存

                var str='{}';//构造自己的JSON http请求的信息及返回的结果
                var data = Fiddler.WebFormats.JSON.JsonDecode(str);
                data.JSONObject["request_method"] = oSession.RequestMethod;
                var requestString = oSession.GetRequestBodyAsString();

                data.JSONObject["request_body"]= requestString;
                data.JSONObject["response_data"] = responseJSON.JSONObject;
                data.JSONObject["url"] = oSession.fullUrl;
                data.JSONObject["response_code"] = oSession.responseCode;
                jsonString = Fiddler.WebFormats.JSON.JsonEncode(data.JSONObject)

                // 保存文件到本地
                var fso;
                var file;
                fso = new ActiveXObject("Scripting.FileSystemObject");
                file = fso.OpenTextFile("E:\\spider_img\\Sessions.dat",8 ,true, true);
                file.writeLine(jsonString);
                file.writeLine("\n");
                file.close();

                // 数据通过post请求发送自己的后台服务保存 
                FiddlerObject.log('2222222222222222'+jsonString); 
                // methods
                var method = "POST";
                var myUrl = 'http://localhost:8000/fiddler'
                var url = myUrl+'?data='+Utilities.UrlEncode(jsonString);
                var protocol = "HTTP/1.1";
                var raw="";
                var selected: Session = oSession;
                raw += method + " " + url + " " + protocol + "\r\n";
  
                raw +="Host:localhost:8000\r\n";
                raw +="Connection: keep-alive\r\n";
                raw +="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n";
                raw +="User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36\r\n";
                raw +="Accept-Encoding: gzip,deflate,sdch\r\n";
                raw +="Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4\r\n";
                raw +="Content-Type: application/json\r\n";
 
                var body= "jsondata=''";
                raw += "\r\n" + body;
                FiddlerObject.utilIssueRequest(raw);
            }
        }

    }

3. View files in session.dat format

Guess you like

Origin blog.csdn.net/weixin_38664232/article/details/108052749