Fiddler 脚本对HTTP请求进行处理

  • Post author:
  • Post category:其他


Fiddler在HTTP编程过程是不可多得的利器,特别是它使用的是代理方式,能够提供非侵入式的HTTP通信报文编程,在Web接口调试方面很方便。特别是它提供使用JScript .NET方式进行功能拓展,让Web前端调试编程变得很容易

先科普

Fiddler, The free web debugging proxy for any browser, system or platform

百度百科的介绍

Fiddler 是用C#写出来的,它包含一个简单却功能强大的基于JScript .NET 事件脚本子系统,它的灵活性非常棒,可以支持众多的http调试任务,并且能够使用.net框架语言进行扩展。

Fiddler官方地址

http://www.telerik.com/fiddler

最近有一个简单的需求,就是使用Fidder把去向特定IP和URL的HTTP请求标注起来,并且保存到指定文件中去。下面是实现的FiddlerScript脚本,供参考

1. 筛选指定条件的请求,在发起前拦截

cript"  >
static function OnBeforeRequest(oSession: Session) {
// 筛选指定条件的请求
if (oSession.HostnameIs("118.145.4.46") &&
oSession.HTTPMethodIs("POST") &&
oSession.uriContains("/member/monitor/customerhold!sum.action")) {
oSession["ui-color"] = "red";
}
}

2.设置菜单栏接口开关

cript"  >
// 菜单栏名称
public static RulesOption("Automatically Dump Summary Data")
var m_AutoDumpSumData: boolean = false;

3.请求完成后,将数据包按时间格式指定文件名并保存

cript"  >
static function OnDone(oSession: Session) {
if (null == Session){
return;
}
// 筛选指定条件的请求
if(m_AutoDumpSumData){
if (oSession.HostnameIs("118.145.4.46") &&
oSession.HTTPMethodIs("POST")){
var dSessionId = oSession.id;

// 时间格式化
var date = new Date();
var month = date.getMonth() + 1;
var strDate = date.getDate();
var strHours = date.getHours();
var strMinutes = date.getMinutes();
var strSeconds = date.getSeconds();
var strMilliSeconds = date.getMilliseconds();

if (month >= 1 && month < br> month = "0" + month;
}
if (strDate >= 0 && strDate < br> strDate = "0" + strDate;
}
if (strHours >= 0 && strHours < br> strHours = "0" + strHours;
}
if (strMinutes >= 0 && strMinutes < br> strMinutes = "0" + strMinutes;
}
if (strSeconds >= 0 && strSeconds < br> strSeconds = "0" + strSeconds;
}
if (strMilliSeconds >= 0 && strMilliSeconds < br> strMilliSeconds = "00" + strMilliSeconds;
}else if(strMilliSeconds >= 10 && strMilliSeconds < br> strMilliSeconds = "0" + strMilliSeconds;
}

var currentdate = date.getFullYear() + month + strDate
+ '_' +strHours + strMinutes + strSeconds + '_'+ strMilliSeconds;

// 保存指定文件名到指定位置
if(oSession.uriContains("/member/monitor/customerhold!sum.action")) {
var outFileName = 'C:/Work/origin/customerhold_sum_' + currentdate +'_'+dSessionId+'.txt';
var parsedFileName = 'C:/Work/parsed/customerhold_sum_' + currentdate +'_'+dSessionId+'.txt';
oSession.SaveResponseBody(outFileName);

// 执行文件处理
System.Diagnostics.Process.Start("C:/Work/parser/Parser.exe",outFileName+" "+parsedFileName);
}
}
}

完整的源代码

cript"  >
import System;
import System.Windows.Forms;
import Fiddler;

// INTRODUCTION
//
// Well, hello there!
//
// Don't be scared! :-)
//
// This is the FiddlerScript Rules file, which creates some of the menu commands and
// other features of Fiddler. You can edit this file to modify or add new commands.
//
// The original version of this file is named SampleRules.js and it is in the
// \Program Files\Fiddler\ folder. When Fiddler first runs, it creates a copy named
// CustomRules.js inside your \Documents\Fiddler2\Scripts folder. If you make a
// mistake in editing this file, simply delete the CustomRules.js file and restart
// Fiddler. A fresh copy of the default rules will be created from the original
// sample rules file.

// The best way to edit this file is to install the FiddlerScript Editor, part of
// the free SyntaxEditing addons. Get it here: http://fiddler2.com/r/?SYNTAXVIEWINSTALL

// GLOBALIZATION NOTE: Save this file using UTF-8 Encoding.

// JScript.NET Reference
// http://fiddler2.com/r/?msdnjsnet
//
// FiddlerScript Reference
// http://fiddler2.com/r/?fiddlerscriptcookbook

class Handlers
{
// *****************
//
// This is the Handlers class. Pretty much everything you ever add to FiddlerScript
// belongs right inside here, or inside one of the already-existing functions below.
//
// *****************

// The following snippet demonstrates a custom-bound column for the Web Sessions list.
// See http://fiddler2.com/r/?fiddlercolumns for more info
/*
public static BindUIColumn("Method", 60)
function FillMethodColumn(oS: Session): String {
return oS.RequestMethod;
}
*/

// The following snippet demonstrates how to create a custom tab that shows simple text
/*
public BindUITab("Flags")
static function FlagsReport(arrSess: Session[]):String {
var oSB: System.Text.StringBuilder = new System.Text.StringBuilder();
for (var i:int = 0; i
{
oSB.AppendLine("SESSION FLAGS");
oSB.AppendFormat("{0}: {1}\n", arrSess[i].id, arrSess[i].fullUrl);
for(var sFlag in arrSess[i].oFlags)
{
oSB.AppendFormat("\t{0}:\t\t{1}\n", sFlag.Key, sFlag.Value);
}
}
return oSB.ToString();
}
*/

// You can create a custom menu like so:
/*
QuickLinkMenu("&Links")
QuickLinkItem("IE GeoLoc TestDrive", "http://ie.microsoft.com/testdrive/HTML5/Geolocation/Default.html")
QuickLinkItem("FiddlerCore", "http://fiddler2.com/fiddlercore")
public static function DoLinksMenu(sText: String, sAction: String)
{



版权声明:本文为yctccg原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。