眉標=Fiddler HTTP Debugger 副標=HTTP專屬偵錯工具 - Fiddler 大標=Microsoft Fiddler延伸開發介紹 作者=文/李明儒 =========程式========= 程式1 將新聞照Redirect成指定的照片 static function OnBeforeRequest( oSession:Fiddler.Session) { if (m_NoAD && (oSession.host.indexOf("ad.")>-1 || //… Fiddler有一段預設的程式碼,此處省略 … oSession.oFlags["x-breakrequest"] = "yup"; } //以下是我們加入的程式碼 var rnd : Random = new Random(); //稍後用亂數選照片 var rndPicNo : String = rnd.Next(1,4).ToString(); if (oSession.url.toLowerCase().indexOf( "news.yam.com/news_photo")>-1) { //比對URL為新聞照片時,換為自訂的照片網址 oSession.url= "labweb/lifephotos/P"+rndPicNo+".jpg"; } } =========程式========= =========程式========= 程式2 改變Google News的網頁開啟行為 static function OnBeforeResponse( oSession:Fiddler.Session) { if (oSession.url.toLowerCase().indexOf( "news.google.com.tw/nwshp")>-1) { oSession["ui-color"] = "red"; //內容有經GZIP壓縮, 先解壓縮 oSession.utilDecodeResponse(); var html : String = Encoding.UTF8.GetString( oSession.responseBodyBytes); //將target=_blank換成target=_self html = html.Replace("target=_blank", "target=_self"); oSession.responseBodyBytes= Encoding.UTF8.GetBytes(html); } } =========程式========= =========程式========= 程式3 可修改MSN送出內容的程式碼 (寫於onBeforeRequest事件中) if (oSession.url.toLowerCase().indexOf( "/gateway/gateway.dll?sessionid=")>-1){ oSession["ui-color"]="red"; //請import System.Text[.RegularExpressions] //取出文字內容 var reqText : String = Encoding.UTF8.GetString( oSession.requestBodyBytes); var msnHeader : Match = Regex.Match(reqText, "(?ims)^MSG \\d+ N (?\\d+)"); if (msnHeader.Success) { //發現傳訊內容 //由MSN標頭列的長度取出內容 var msgBody : String = Encoding.UTF8.GetString( Encoding.UTF8.GetBytes( reqText.Substring( msnHeader.Index+msnHeader.Length+2 ) ), 0, int.Parse(msnHeader.Groups["len"].Value) ); //檢查是否含有[$$...$$]Tag var parserTag : Match = Regex.Match(msgBody, "(?ims)[[][$][$](?.+?)[$][$][]]"); if (parserTag.Success) { var tagName : String = parserTag.Groups["tagName"].Value; var info : String = ""; //此段實際應由資料庫取值 //此處以switch case模擬 switch (tagName) { case "HD": info = "Seagate 250G/7200轉/IDE/DMA-100/8MB: NT$3,700\n\r"+ "Seagate 250G/7200轉/SATA150/8MB: NT$ 4,150\n\r"+ "WD 250G/7200轉/SATAII300/8MB: NT$ 3,950\n\r"; break; case "RAM": info = "金士頓 1GB/DDR400: NT$ 3,2200\n\r"+ "威剛 1GB/DDR400: NT$ 3,200\n\r"+ "創見 1GB/DDR400: NT$ 3,200\n\r" break; } //做字串替換 var newMsgBody : String = msgBody.Replace(parserTag.Value,info); //變更MSN Header長度 var origMsnPack : String = msnHeader.Value + "\n\r" + msgBody; var newMsnPack : String = msnHeader.Value.Replace( " N "+msnHeader.Groups["len"].Value, " N "+Encoding.UTF8.GetBytes( newMsgBody).Length.ToString() ) + "\n\r" + newMsgBody; //更換Request內容 reqText = reqText.Replace( origMsnPack, newMsnPack); oSession.requestBodyBytes = Encoding.UTF8.GetBytes(newMsnPack); //更新Content-Length oSession.oRequest["Content-Length"] = oSession.requestBodyBytes.Length; } } } =========程式=========