前两天因项目须要,简单看了一下FiddlerScript,功能挺强的。今天有时间细致看一下,做个笔记。

改动Request或Response

改动Request和Response要在FiddlerScript中的OnBeforeRequest和OnBeforeResponse函数中加入规则就可以。OnBeforeRequest函数是在每次请求之前调用。OnBeforeResponse函数是在每次响应之前调用。

1、加入请求头Header

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

2、删除Response的Header

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

3、将请求从一个页面转发到同一Server上的还有一页面

if (oSession.PathAndQuery=="/hello/hello.html") {
oSession.PathAndQuery="/hello/index.html";
}

注意:oSession.PathAndQuery的值为fiddler中session列表中的Url:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29vbGNhb3Nq/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">

即图中红色标注出来的部分。图中黄色标注出来的部分有点特殊。host为Tunnel to ,url为还有一host。

查看该请求的Header为:

这样的特殊情况会在以下还有样例。

上面的样例,拦截请求地址为/hello/hello.html的请求,并将其转发到同样Server的/hello/index.html

4、将请求转发到同样port号的不同server(改动请求的Host)

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


这个样例是将发送到百度的请求转发到新浪。则会提示页面不存在。

这里仅仅是改变了host,并不改变后面的地址。因此。假设在新浪上不存在对应的页面。如以下图片所看到的:

假设我訪问的是例如以下地址:http://www.baidu.com/link?url=CQuVpjo9u9UQADcstwECPEmrziPMk5u5H9PlRN2TbWLkKZaxafVER2X8OEYzovr-yasX2Fwcgj0NANBtKVj0gN78jNJ3bXTmIsTeBk7hXem

则结果例如以下:(该页面实际是存在的,是百度搜索出来的结果页面,被fiddler转发到新浪。可是新浪上不存的此页面)

5、将请求转发到不同port号,不同Server

    if (oSession.host=="192.168.0.70:8080") {
oSession.host="192.168.0.69:8020";
}

这个样例是将发送到192.168.0.70:8080的请求转发到192.168.0.69:8020,这里仅仅是改变host。并不改变后面的请求地址。比如,做以上的规则后。我请求的是:

http://192.168.0.70:8080/hello/hello.html

而实际我项目部署到的是:192.168.0.69:8020

6、将全部请求从一个server转发到还有一个server,包含Https

    // Redirect traffic, including 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";

7、Simulate the Windows HOSTS file, by pointing one Hostname to a different IP address. (Retargets without changing the request's Host header)

    // 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
}

8、Retarget requests for a single page to a different page, potentially on a different server. (Retargets by changing the request's Host header)

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

9、Prevent upload of HTTP Cookies

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

10、Decompress and unchunk a HTTP response, updating headers if needed

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

11、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>');
}

12、Case insensitive Search of response HTML.

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

13、Remove all DIV tags (and content inside the DIV tag)

    // 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);
}

14、Pretend your browser is the GoogleBot webcrawler

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

15、Request Hebrew content

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

16、Deny .CSS requests

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

17、Simulate HTTP Basic authentication (Requires user to enter a password before displaying web content.)

    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");
}

18、Respond to a request with a file loaded from the \Captures\Responses folder (Can be placed in OnBeforeRequest or OnBeforeResponse function)

    if (oSession.PathAndQuery=="/version1.css") {
oSession["x-replywithfile"] ="version2.css";
}

以上样例我并没有都实践。仅仅实践了中间几个地址转发的,由于如今须要用。剩下的请大家有须要的自己实践吧。

FiddlerScript学习一:改动Request或Response的更多相关文章

  1. JavaWeb学习笔记四 request&response

    HttpServletResponse 我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应res ...

  2. 转 #HTTP协议学习# (一)request 和response 解析

    http://www.cnblogs.com/bukudekong/p/3834020.html #HTTP协议学习# (一)request 和response 解析   注:本文转自:http:// ...

  3. #HTTP协议学习# (一)request 和response 解析

    注:本文转自:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html , 粉字[]内内容为个人笔记 当今web程序的开发技术真是 ...

  4. JavaWeb学习总结(三)response与request

    一.response response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletResponse.在客户端发出每个请求时,服务器 ...

  5. Java web课程学习之Request和Response

    request和response l HttpServletRequest l 请求转发 l HttpServletResponse l 请求重定向   请求流程 每次请求service(),都会由容 ...

  6. Web jsp开发学习——终极解决jsp中request和response中文乱码的问题(加个过滤器)

    中文乱码真的很烦人的.而且每次都要写,可麻烦了,而且有时候写了还不一定管用,所以我们可以试试过滤器 1.每个jsp头上当然要写上utf8啦 <%@ page language="jav ...

  7. javaweb学习总结二十五(response对象的用法一)

    一:Reponse对象的概念 当客户端发送http请求时,服务器端会对每一次请求,创建request对象和response对象. response对象包括三个部分:响应头.响应状态码以及响应体 二:r ...

  8. [Rails] 从 Request 到 Response(2)

    本文翻译自:Rails from Request to Response 系列:个人选择了自己感兴趣的部分进行翻译,需要阅读原文的同学请戳前面的链接. 第二部分 路由(Routing) Blog::A ...

  9. 关于request、response转发与重定向的简述

    在做页面的请求与响应的时候我们多用request与response进行操作,而我们大家也知道,request是表示用户发向服务器的请求,而response是对用户请求的一个响应. 关于转发和重定向,通 ...

随机推荐

  1. Android获取系统时间yyyyMMddHHmmssSSS

    代码改变世界 public String testTime1() throws ParseException { String DEFAULT_TIME_FORMAT = "yyyy-MM- ...

  2. [HDU3516] Tree Construction [四边形不等式dp]

    题面: 传送门 思路: 这道题有个结论: 把两棵树$\left[i,k\right]$以及$\left[k+1,j\right]$连接起来的最小花费是$x\left[k+1\right]-x\left ...

  3. Python 安装MySQLdb模块遇到报错及解决方案:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory

    一.问题 系统:win7 64位 在下载MySQL-python-1.2.5.zip,使用python setup.py install 安装时,出现以下报错: _mysql.c(42) : fata ...

  4. JS 改变鼠标样式

    此片记录如何改变鼠标样式: document.body.style.cursor = "url(resource/pic/icons/magnifier3.cur) 12 12,crossh ...

  5. input type file上传文件之后清空内容。

    上次写过如何上传文件,上传成功之后,会出现一些问题. 当我打开上传的文件,但是没有点击上传,然后关闭弹窗,接着继续上传刚才的那个文件.为了满足产品组的要求,我们一般都会把样式进行一定的覆盖. 但这就会 ...

  6. client、offset、scroll

    1. client 客户区大小 1.1. clientHeight和clientWidth element.clientHeight表示元素可视区域的高度,包括可视区域中元素的 CSS height+ ...

  7. 虚拟 ​router 原理分析

    上一节我们创建了虚拟路由器“router_100_101”,并通过 ping 验证了 vlan100 和 vlan101 已经连通. 本节将重点分析其中的原理. 首先我们查看控制节点的 linux b ...

  8. spring一些总结

    Spring中三种实例化bean的方法: 1)使用类构造器   <bean id="orderService" class="cn.itcast.OrderServ ...

  9. hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜

    题目链接 题意 给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\). 思路 暴搜. 搜索的时候判断要加进 ...

  10. 了解 Oracle Berkeley DB 可以为您的应用程序带来 NoSQL 优势的原因及方式。

    将 Oracle Berkeley DB 用作 NoSQL 数据存储 作者:Shashank Tiwari 2011 年 2 月发布 “NoSQL”是在开发人员.架构师甚至技术经理中新流行的一个词汇. ...