FiddlerScript学习一:改动Request或Response

前两天因项目须要,简单看了一下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:

即图中红色标注出来的部分。图中黄色标注出来的部分有点特殊。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";
    }

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

时间: 2024-08-28 19:02:25

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

FiddlerScript学习一:修改Request或Response

前两天因项目需要,简单看了一下FiddlerScript,功能挺强的,今天有时间仔细看一下,做个笔记. 修改Request或Response 修改Request和Response要在FiddlerScript中的OnBeforeRequest和OnBeforeResponse函数中添加规则即可.OnBeforeRequest函数是在每次请求之前调用,OnBeforeResponse函数是在每次响应之前调用. 1.添加请求头Header oSession.oRequest["NewHeaderNa

JavaWeb学习笔记四 request&amp;response

HttpServletResponse 我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应response.service方法中的response的类型是ServletResponse,而doGet/doPost方法的response的类型是HttpServletResponse,HttpServletResponse是ServletResponse的子接口,功能和方法更加强大. respon

Struts学习笔记(三)struts2中获得request、response和session的三种方法

struts2中获得request.response和session的三种方法   (1)非IoC方式 方法一:使用org.apache.struts2.ActionContext类,通过它的静态方法getContext()获取当前Action的上下文对象. ActionContext ctx = ActionContext.getContext(); ctx.put("liuwei", "andy"); //request.setAttribute("l

Request和Response对象

最早接触Request和Response对象的时候是听王勇老师将Servlet的时候,我们编写一个Servlet类.我们在浏览器端发送一个url请求之后,会调用Servlet类对这个请求做相应的处理,Servlet类就是通过重写的service()方法来实现的,而我们知道的Request和Response对象就是作为service()方法的参数传入的.   1.整体印象 2.Request对象 Request对象的主要职责是获得HTTP请求中封装的信息,获得url和表单参数:还有一个功能就是对页

day03-java web之request、response

请求响应流程图 response 1        response概述 response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletResponse.在客户端发出每个请求时,服务器都会创建一个response对象,并传入给Servlet.service()方法.response对象是用来对客户端进行响应的,这说明在service()方法中使用response对象可以完成对客户端的响应工作. response对象的功能分为以下

ASP.NET之Request和Response对象

经过了牛腩新闻公布系统和html的学习对B/S开发的流程有了些理解.前面尽管用到了非常多知识.但对制作网页仅仅能说知其然.当学到asp.net视频中的解说才干够说開始知其所以然了. 今天来说说client与server之间进行交流用到的Request和Response对象. Request:用户在client使用Web浏览器向Web应用程序发出请求时,会将client信息发给server.server收到一个HTTP请求,包括了全部查询字符串參数或表单參数.Cookie数据以及浏览器信息. 在a

ASP.NET内置对象Request 和Response

ASP.NET提供了内置对象有Page.Request.Response.Application.Session.Server.Mail和Cookies.这些对象使用户更容易收集通过浏览器请求发送的信息.响应浏览器以及存储用户信息,以实现其他特定的状态管理和页面信息的传递. 下面针对request和response做简单介绍,大家多多指正,补充. Request 读取客户端在Web请求期间发送的值,即是读取浏览器传来的请求报文数据. Request对象主要是让服务器取得客户端浏览器的一些数据,包

servlet中的request和response

利用3到4天的时间,把servlet中的request和response对象视频看了一遍.看完之后,自己总结出来了一些知识点. 学习一个对象的三部曲:首先查看API文档,了解以及掌握对象的一些常用的方法用法:其次就是解决一些中文乱码之类的问题:最后就是该对象的应用了. 好了,先来说说response对象吧. 1.解决用outputStream输出中文的乱码问题. //1.程序的数据以什么码表输出了,程序就要控制浏览器以什么码表打开 response.setHeader("content-type

request与response对象.

request与response对象. 1. request代表请求对象 response代表的响应对象. 学习它们我们可以操作http请求与响应. 2.request,response体系结构. 在我们Servlet中使用的request与response HttpServletRequest接口------>父接口  ServletRequest HttpServletResponse接口----->父接口  ServletResponse 在tomcat中它采用门面模式对requst,r