Fiddler 修改请求Request和响应Response

要对web请求和响应进行自定义更改,请使用FiddlerScript向Fiddler的OnBeforeRequest或OnBeforeResponse函数添加规则。哪个函数合适取决于代码使用的对象:on before request在每个请求之前调用,onbeforereresponse在每个响应之前调用。注:

无法访问OnBeforeRequest中的响应对象,因为它们尚未创建。

可以在OnBeforeResponse中使用来自请求的对象;但是,服务器将看不到您对这些对象所做的任何更改,因为它已经收到请求。

添加请求头

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

删除响应头

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

将一个页面的请求更改为同一服务器上的其他页面

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

将一台服务器的所有请求指向另一台服务器上的同一端口

if (oSession.HostnameIs("www.bayden.com")) {
  oSession.hostname="test.bayden.com";
}

将一个端口的所有请求指向不同服务器上的不同端口

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

将一台服务器的所有请求指向另一台服务器,包括HTTPS隧道

// 重定向流量,包括HTTPS隧道
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"; 

通过将一个主机名指向不同的IP地址来模拟Windows主机文件。(在不更改请求的主机头的情况下重定目标)

// 所有对subdomain.example.com的请求都应指向位于128.123.133.123的开发服务器
if (oSession.HostnameIs("subdomain.example.com")){
oSession.bypassGateway = true;                   // Prevent this request from going through an upstream proxy
oSession["x-overrideHost"] = "128.123.133.123";  // DNS name or IP address of target server
}

将单个页面的请求重定目标到不同的页面,可能在不同的服务器上。(通过更改请求的主机头来重定目标)

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

阻止上载HTTP Cookies

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

解压缩并取消对HTTP响应的解压缩,如果需要,则更新头

// 从响应中移除任何压缩或分块,以便更容易操作
oSession.utilDecodeResponse();

Search and replace in HTML.

if (oSession.HostnameIs("www.bayden.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标记内的内容)
// 如果内容类型是HTML,那么删除所有DIV标记
if (oSession.oResponse.headers.ExistsAndContains(“Content-Type”, “html”)){
// Remove any compression or chunking
oSession.utilDecodeResponse();
var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);

  // 用空字符串替换DIV标记的所有实例
  var oRegEx = /<div[^>]*>(.*?)<\/div>/gi;
  oBody = oBody.replace(oRegEx, "");

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

假设你的浏览器是GoogleBot的webcrawler

oSession.oRequest["User-Agent"]="Googlebot/2.X (+http://www.googlebot.com/bot.html)";

Request Hebrew content

oSession.oRequest["Accept-Language"]="he";

Deny .CSS requests

if (oSession.uriContains(".css")){
 oSession["ui-color"]="orange"; 
 oSession["ui-bold"]="true";
 oSession.oRequest.FailSession(404, "Blocked", "Fiddler blocked CSS file");
}

模拟HTTP基本身份验证(要求用户在显示web内容之前输入密码。)

if ((oSession.HostnameIs("www.example.com")) && 
 !oSession.oRequest.headers.Exists("Authorization")) 
{
// 通过使响应正文超过512个字符,防止IE的“友好错误消息”隐藏错误消息。
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");
}

使用从\Captures\Responses文件夹加载的文件响应请求(可以放在OnBeforeRequest或OnBeforeResponse函数中)

if (oSession.PathAndQuery=="/version1.css") {
  oSession["x-replywithfile"] ="version2.css";
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值