Fiddler修改请求或者响应

FiddlerScript允许你在Fiddler Classic中修改Web请求和响应。你可以通过添加规则到OnBeforeRequestOnBeforeResponse函数来实现自定义修改。
在这里插入图片描述

选择哪个函数取决于你的代码使用的对象类型:

  • OnBeforeRequest在每个请求之前被调用。
  • OnBeforeResponse在每个响应之前被调用。

需要注意的是,在OnBeforeRequest中无法访问响应对象,因为此时它们尚未创建。同时,虽然可以在OnBeforeResponse中使用请求对象,但是对这些对象所做的任何更改都不会被服务器看到,因为它已经接收了请求。

下面是一些示例操作,你可以使用FiddlerScript来执行:

  • 添加请求头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隧道if (oSession.HTTPMethodIs("CONNECT") && (oSession.PathAndQuery == "www.example.com:443")) { oSession.PathAndQuery = "beta.example.com:443"; }
  • 模拟Windows HOSTS文件,指向不同的IP地址if (oSession.HostnameIs("subdomain.example.com")){ oSession.bypassGateway = true; oSession["x-overrideHost"] = "128.123.133.123"; }
  • 重定向特定页面的请求至另一服务器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();
  • 在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标签及其内容if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html")){ oSession.utilDecodeResponse(); var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes); var oRegEx = /<div[^>]*>(.*?)<\/div>/gi; oBody = oBody.replace(oRegEx, ""); oSession.utilSetResponseBody(oBody); }
  • 模拟GoogleBot Web爬虫oSession.oRequest["User-Agent"]="Googlebot/2.X (+http://www.googlebot.com/bot.html)";
  • 请求希伯来语内容oSession.oRequest["Accept-Language"]="he";
  • 拒绝CSS请求if (oSession.uriContains(".css")){ oSession["ui-color"]="orange"; oSession["ui-bold"]="true"; oSession.oRequest.FailSession(404, "Blocked", "Fiddler Classic blocked CSS file"); }
  • 模拟HTTP基本认证(要求用户在显示网页内容前输入密码):if ((oSession.HostnameIs("www.example.com")) && !oSession.oRequest.headers.Exists("Authorization")){...}
  • 响应请求时使用\Captures\Responses文件夹中的文件if (oSession.PathAndQuery=="/version1.css") { oSession["x-replywithfile"] ="version2.css"; }

这些脚本示例可以帮助你根据需求修改网络流量。请确保在实施这些修改时考虑所有相关的安全性和功能影响。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知识的宝藏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值