要对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";
Modifying a Request or ResponseTo make custom changes to web requests and responses, use FiddlerScript to add rules to Fiddler’s OnBeforeRequest or OnBeforeResponse function. Which function is approp...
完事了之后,按下Ctrl+F来查找OnBeforeRequest这个方法,完事呢,在这个方法的末尾添加如下代码:
//过滤无关请求,只关注特定请求
if (oSession.fullUrl.Contains(“填写需要抓取的域名”)) {
var fso;
Fiddler有两个重要的需要掌握的函数(方法):
OnBeforeRequest & OnBeforeResponse,根据英文字面意思不难理解
OnBeforeRequest :请求前函数。请求经过Fiddler发送给服务器之前会先调用这个函数,因此,如果想要修改请求,可以写在这个函数里;
OnBeforeResponse:响应后函数。从服务器读完响应后,且在将响应发送给客户端之前执行的。通过修改这个
然后在方法的后面追加如下代码:
var isJson=oSession.ResponseHeaders.ExistsAndContains("Content-Type","json");
if (oSe...
1、拦截http请求
使用Fiddler进行HTTP断点调试是fiddler一强大和实用的工具之一。通过设置断点,Fiddler可以做到:
①修改HTTP请求头信息。例如修改请求头的UA,Cookie,Referer信息,通过“伪造”相应信息达到相应的目的(调试,模拟用户真实请求等)。
②构造请求数据,突破表单的限制,随意提交...
下面我先通过两个具体例子来进行说明
修改fiddler的CustomRules.js的规则
1.将
请求的域名转发到带端口号的host上,并不改变后面的
请求地址.(注意:这个是发
请求,所以要放在
请求之前)
方法步骤:
(1)
fiddler,选择Rules—customize
(2)记事本打开CustomRules.js文件
这个也是第一次使用,只是单纯的为了在本地代理测试一下修改的文件是否正常显示,功能是否完善;
==============================================================================
工作中难免遇到很大的一个项目且已经上线,当其中某一部分出现问题,线下又无法看到修改后的效果,更不可能在不知道效果的情况下就上线然后看效果是否符合预