fiddler高級用法,手動修改替換請求和響應

手動修改fiddle的請求和響應
Rules->Custon Rules,或按Ctrl+R鍵,編輯 CustomRules.js 代碼文件,在OnBeforeRequest函數裏修改代碼:

添加請求 header(頭)

oSession.oRequest["NewHeaderName"] = "New header(頭) value";

刪除響應 header(頭)

oSession.oResponse.headers.Remove("Set-Cookie");

改變請求參數

if (oSession.PathAndQuery=="/version1.css") {
oSession.PathAndQuery="/version2.css";
}

替換請求url指向

if (oSession.HostnameIs("www.baidu.com")) {
oSession.hostname="www.google.com";
}

替換請求url指向和端口

if (oSession.host=="www.baidu.com:8080") {
oSession.host="test.baidu.com:9090";
}

替換請求url指向包含HTTPS tunnels


if (oSession.HTTPMethodIs("CONNECT") && (oSession.PathAndQuery == "www.example.com:443")) {
oSession.PathAndQuery = "beta.example.com:443";
}


if (oSession.HostnameIs("www.example.com")) oSession.hostname = "beta.example.com";

 

替換網頁和靜態文件

if (oSession.url=="www.example.com/live.js") {
oSession.url = "dev.example.com/workinprogress.js";
}

阻止上載HTTP Cookie

oSession.oRequest.headers.Remove("Cookie");

解壓縮並取消解壓HTTP響應

// Remove any compression or chunking from the response in order to make it easier to manipulate
oSession.utilDecodeResponse();

在HTML中搜索和替換

if (oSession.HostnameIs("www.baidu.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){
oSession.utilDecodeResponse();
oSession.utilReplaceInResponse('<b>','<u>');
}

響應HTML的不區分大小寫搜索.

if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "text/html") && oSession.utilFindInResponse("searchfor", false)>-1){
oSession["ui-color"] = "red";
}

刪除所有DIV標記(以及DIV標記內的內容)

// If content-type is HTML, then remove all DIV tags
if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html")){
// Remove any compression or chunking
oSession.utilDecodeResponse();
var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);

// Replace all instances of the DIV tag with an empty string
var oRegEx = /<div[^>]*>(.*?)<\/div>/gi;
oBody = oBody.replace(oRegEx, "");

// Set the response body to the div-less string
oSession.utilSetResponseBody(oBody); 
}

模擬HTTP基本身份驗證(要求用戶在顯示web內容之前輸入密碼。)

if ((oSession.HostnameIs("www.example.com")) && 
!oSession.oRequest.headers.Exists("Authorization")) 
{
// Prevent IE's "Friendly Errors Messages" from hiding the error message by making response body longer than 512 chars.
var oBody = "<html><body>[Fiddler] Authentication Required.<BR>".PadRight(512, ' ') + "</body></html>";
oSession.utilSetResponseBody(oBody); 
// Build up the headers
oSession.oResponse.headers.HTTPResponseCode = 401;
oSession.oResponse.headers.HTTPResponseStatus = "401 Auth Required";
oSession.oResponse["WWW-Authenticate"] = "Basic realm=\"Fiddler (just hit Ok)\"";
oResponse.headers.add("Content-Type", "text/html");
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章