[TOC]
>[success] # 参数的总结
~~~
1.
~~~
>[danger] ##### 在请求头中增加/更改参数
~~~
oSession.oRequest["NewHeaderName"] = "New header value";
~~~
* 效果图
![](https://box.kancloud.cn/f4846b91a566b27ca124cde9582c4f97_516x175.png)
>[danger] ##### 新建cookies
~~~
oSession.oRequest.headers.Add("Cookie", "username=testname;testpassword=P@ssword1");
~~~
>[danger] ##### 清空请求头指定参数
~~~
oSession.oResponse.headers.Remove("Set-Cookie");
~~~
>[danger] ##### 修改cookie
~~~
if (oSession.HostnameIs('www.example.com') &&
oSession.uriContains('pagewithCookie') &&
oSession.oRequest.headers.Contains("Cookie"))
{
var sCookie = oSession.oRequest["Cookie"];
// 用replace方法或者正则表达式的方法去操作cookie的string
sCookie = sCookie.Replace("cookieName=", "ignoreme=");
oSession.oRequest["Cookie"] = sCookie;
}
~~~
>[danger] ##### 利用脚本进行请求篡改(必须使用请求体的)
~~~
if(oSession.uriContains("www.baidu.com"))
{
// 获取Request 中的body字符串
var strBody=oSession.GetRequestBodyAsString();
// 用正则表达式或者replace方法去修改string
strBody=strBody.replace("1111","2222");
// 弹个对话框检查下修改后的body
FiddlerObject.alert(strBody);
// 将修改后的body,重新写回Request中
oSession.utilSetRequestBody(strBody);
}
~~~
* 提供的上面缩写的方法
~~~
oSession.utilReplaceInRequest("1111", "2222");
~~~
>[danger] ##### 更改相同服务器的请求
~~~
// 可以理解在同一个host
if (oSession.PathAndQuery=="/version1.css") {
oSession.PathAndQuery="/version2.css";
}
~~~
>[danger] ##### 更改host
~~~
if (oSession.HostnameIs("www.bayden.com")) {
oSession.hostname="test.bayden.com";
}
~~~
>[danger] ##### 更改端口
~~~
if (oSession.host=="www.bayden.com:8080") {
oSession.host="test.bayden.com:9090";
}
~~~
>[danger] ##### 更改重定向ip地址
~~~
// All requests for subdomain.example.com should be directed to the development server at 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
}
~~~
>[danger] ##### 链接跳转重定向
~~~
if (oSession.url=="www.example.com/live.js") {
oSession.url = "dev.example.com/workinprogress.js";
}
~~~
>[danger] ##### 禁止请求时带上cookies
~~~
oSession.oRequest.headers.Remove("Cookie");
~~~
>[danger] ##### 更改请求语言
~~~
oSession.oRequest["Accept-Language"]="he";
~~~
>[danger] ##### 禁止请求css
~~~
if (oSession.uriContains(".css")){
oSession["ui-color"]="orange";
oSession["ui-bold"]="true";
oSession.oRequest.FailSession(404, "Blocked", "Fiddler blocked CSS file");
}
~~~
>[success] # 响应 -- OnBeforeResponse
~~~
~~~
>[danger] ##### utilDecodeResponse -- 解压响应信息
~~~
// Remove any compression or chunking from the response in order to make it easier to manipulate
oSession.utilDecodeResponse();
~~~
>[danger] ##### utilReplaceInResponse-- 更改响应中的html
~~~
if (oSession.HostnameIs("www.bayden.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){
oSession.utilDecodeResponse();
oSession.utilReplaceInResponse('<b>','<u>');
}
~~~
>[dasnger] ##### -- 利用正则删除响应中的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);
}
~~~
>[success] # 参考文档
<a href="http://docs.telerik.com/fiddler/KnowledgeBase/FiddlerScript/ModifyRequestOrResponse"> 参考文章</a>