Sei sulla pagina 1di 7

3/25/2014

Fiddler Web Debugger - Importing and Exported from Fiddler with .NET Code

Ge t the NEW book !

Get Fiddler!

Addons

Help & Developer Documentation Info

Discuss

Contact

Importers and Exporters

Introduction
Fiddle r 2.2.9.5 introduce d the ISe ssionIm port and ISe ssionEx port inte rface s, allowing you to im port traffic to Fiddle r and e x port it to othe r file form ats. Fiddle r ships with a num be r of the se "transcode rs" by de fault, but you can also write your own using any .NET language .

Prerequisites
Fiddle r v2.x loads only .NET CLR v2.0 asse m blie s; use Visual Studio 2005+ to com pile your e x te nsion. If you use Visual Studio 2010 or late r, you must change your proje ct to targe t the .NET2.0/3.5 fram e work or Fiddle r will not load your e x te nsion. Fiddle r itse lf re quire s only that the use r have .NET Framework 2.0 SP1 installe d. You may have your e x te nsions targe t the .NET Fram e work 3.5 (which include s Linq) since that fram e work also (confusingly) runs on the v2.0 C LR but you MUST yourse lf e nsure that the use r has the re quire d Fram e work ve rsion installe d BEFO R E you install your e x te nsion, or a use r with only the olde r Fram e work will crash on boot. You should also e nsure your proje ct targe ts A nyCPU to e nsure that it work s prope rly with 64bit Fiddle r. Le arn m ore about Building asse m blie s to run in both Fiddle r v2 and v4

Debugging your Extensions


Extension developers should set the f i d d l e r . d e b u g . e x t e n s i o n s . s h o w e r r o r s preference to True ensure that exceptions and other extension-related errors are not silently caught. Extension developers should set the f i d d l e r . d e b u g . e x t e n s i o n s . v e r b o s e preference to True to spew logging information to the Log tab. If you write to me complaining that your extension doesn't work and you have not first set these preferences before testing, I will tease you mercilessly.

Direct Fiddler to load your Transcoder assemblies


Fiddle r loads e x te nsion asse m bly DLLs from the % P r o g r a m F i l e s % \ F i d d l e r 2 \ I m p o r t E x p o r t \ and % U S E R P R O F I L E % \ D o c u m e n t s \ F i d d l e r 2 \ I m p o r t E x p o r t \ folde rs. Install to the %Program File s% location to m ak e your e x te nsions available to all use rs on the m achine , or the %Use rProfile % folde r to m ak e the e x te nsion available only to the curre nt use r. In addition to placing your Transcode r DLLs in the appropriate folde r, you m ust also m ark your asse m bly to indicate the m inim um ve rsion of Fiddle r re quire d for your Asse m bly to load corre ctly.

http://fiddlerbook.com/Fiddler/dev/ISessionExport.asp

1/7

3/25/2014

Fiddler Web Debugger - Importing and Exported from Fiddler with .NET Code

Se t the F i d d l e r . R e q u i r e d V e r s i o n attribute in your A s s e m b l y I n f o . c s file (or e lse whe re in your code ), as follows:

u s i n gF i d d l e r ; / /T h i sT r a n s c o d e rr e q u i r e sF i d d l e r2 . 2 . 9 . 7 +b e c a u s et h a ti sw h e nt h e / /I S e s s i o n I m p o r ta n dI S e s s i o n E x p o r ti n t e r f a c e sw e r ef i n a l i z e d . [ a s s e m b l y :F i d d l e r . R e q u i r e d V e r s i o n ( " 2 . 2 . 9 . 7 " ) ] If Fiddle r finds a R e q u i r e d V e r s i o nattribute that indicate s a late r ve rsion of Fiddle r is re quire d, the use r will be notifie d that a late r ve rsion of Fiddle r is re quire d to use your e x te nsion. Asse m blie s which do not contain a R e q u i r e d V e r s i o nattribute are sile ntly ignore d.

Threading, FiddlerCore, and other notes


C urre ntly, the ISe ssionIm porte r and ISe ssionEx porte r inte rface s are calle d on the MAIN UI thre ad. This is alm ost ce rtain to change in the future , so you should e nsure that your classe s are thre ad safe and that the y do not atte m pt to dire ctly m anipulate the Fiddle r UI. Manipulation of the Fiddle r UI is furthe r ill-advise d be cause Fiddle r itse lf m ay not be loade d; Fiddle rC ore m ay be hosting your Im porte r/Ex porte r dire ctly. In orde r to support Fiddle rC ore , it's advise d that you support the Filename k e y (with string value of a fully-qualifie d path) in the dictOptions param e te r, and conside r supporting a Silent key (value as boole an) as we ll.

The ISessionImporter Interface


Ex te nsions that im ple m e nt the I S e s s i o n I m p o r t e rinte rface (which im ple m e nts the I D i s p o s a b l e inte rface ) are calle d whe n the use r use s the File > Im port m e nu option.

p u b l i ci n t e r f a c eI S e s s i o n I m p o r t e r:I D i s p o s a b l e

S e s s i o n [ ]I m p o r t S e s s i o n s ( s t r i n gs I m p o r t F o r m a t ,D i c t i o n a r y < s t r i n g ,

o b j e c t >

d i c t O p t i o n s ,
E v e n t H a n d l e r < P r o g r e s s C a l l b a c k E v e n t A r g s >e v t P r o g r e s s N o t i f i c a t i o n s ) ; } The m e thod should re turn an array of Se ssion obje cts cre ate d from the Im port of the data. The dictO ptions dictionary m ay be null, or m ay contain a se t of string-k e ye d obje cts. Most im porte rs support spe cification of a file nam e lik e so d i c t O p t i o n s [ " F i l e n a m e " ]=" C : \ \ t e s t . f i l e "

The ISessionExporter Interface


Ex te nsions that im ple m e nt the I S e s s i o n E x p o r t e rinte rface (which im ple m e nts the I D i s p o s a b l e inte rface ) are calle d whe n the use r use s the File > Ex port m e nu option.

p u b l i ci n t e r f a c eI S e s s i o n E x p o r t e r:I D i s p o s a b l e

b o o lE x p o r t S e s s i o n s ( s t r i n gs E x p o r t F o r m a t ,S e s s i o n [ ]o S e s s i o n s ,

D i c t i o n a r y < s t r i n g ,o b j e c t >d i c t O p t i o n s ,
E v e n t H a n d l e r < P r o g r e s s C a l l b a c k E v e n t A r g s >e v t P r o g r e s s N o t i f i c a t i o n s ) ; } The m e thod should re turn True if the e x port of the se ssions passe d via the oSe ssions param e te r is succe ssful. The dictO ptions dictionary m ay be null, or m ay contain a se t of string-k e ye d obje cts. Most im porte rs support spe cification of a file nam e lik e so d i c t O p t i o n s [ " F i l e n a m e " ]=" C : \ \ t e s t . f i l e "

The ProgressCallbackEventArgs class


This class is de fine d by Fiddle r and allows you to re port on the progre ss of an im port or e x port ope ration. If the com ple tion ratio cannot be de te rm ine d, sim ply pass 0 or a "gue ss" be twe e n 0 and 1.0.
http://fiddlerbook.com/Fiddler/dev/ISessionExport.asp 2/7

3/25/2014

Fiddler Web Debugger - Importing and Exported from Fiddler with .NET Code

If the C ance l flag is se t on the Progre ssC allback Eve ntArgs obje ct afte r be ing passe d to the e v t P r o g r e s s N o t i f i c a t i o n scallback , im port or e x port should grace fully te rm inate as soon as possible .

p u b l i cc l a s sP r o g r e s s C a l l b a c k E v e n t A r g s :E v e n t A r g s { p u b l i cP r o g r e s s C a l l b a c k E v e n t A r g s ( f l o a tf l C o m p l e t i o n R a t i o ,s t r i n gs P r o g r e s s T e x t ) p u b l i cs t r i n gP r o g r e s s T e x t{g e t ;} p u b l i cs t r i n gP e r c e n t C o m p l e t e{g e t ;} p u b l i cb o o lC a n c e l{g e t ;s e t ;} }

Step-by-step Sample Extension


Here's a trivial extension which modifies the User-Agent string of all outbound requests. 1. 2. 3. 4. 5. 6. Start Visual Studio 2005. Create a new Project of type Visual C# Class Library Right-click the project's References folder in the Solution Explorer Choose the Browse tab and find Fiddler.exe in the C:\Program Files\Fiddler2 folder. Click Ok to add the reference. If you're planning to add to Fiddler's UI 1. Right-click the project's References folder in the Solution Explorer again 2. On the .NET tab, choose System.Windows.Forms. 3. Click Ok to add the reference. 7. In the Solution Explorer, right click the project. Choose Properties. 8. On the Build Events tab, add the following to the Post-build event command line: c o p y" $ ( T a r g e t P a t h ) " " % u s e r p r o f i l e % \ D o c u m e n t s \ F i d d l e r 2 \ I m p o r t E x p o r t \ $ ( T a r g e t F i l e n a m e ) " 9. Modify the default class1.cs (or create a new class) in your project as follows: u s i n gS y s t e m ; u s i n gS y s t e m . I O ; u s i n gS y s t e m . T e x t ; u s i n gS y s t e m . W i n d o w s . F o r m s ; u s i n gF i d d l e r ; u s i n gS y s t e m . D i a g n o s t i c s ; u s i n gS y s t e m . R e f l e c t i o n ; [ a s s e m b l y :A s s e m b l y V e r s i o n ( " 1 . 0 . 0 . 0 " ) ] [ a s s e m b l y :F i d d l e r . R e q u i r e d V e r s i o n ( " 2 . 4 . 0 . 0 " ) ]

[ P r o f f e r F o r m a t ( " T A B S e p a r a t e dV a l u e s " ," S e s s i o nL i s ti nT a b D e l i m i t e d F o r m a t " ) ] [ P r o f f e r F o r m a t ( " C o m m a S e p a r a t e dV a l u e s " , " S e s s i o nL i s ti nC o m m a D e l i m i t e dF o r m a t ;i m p o r ti n t oE x c e lo ro t h e r t o o l s " ) ] p u b l i cc l a s sC S V T r a n s c o d e r :I S e s s i o n E x p o r t e r / /E n s u r ec l a s si sp u b l i c ,o r F i d d l e rw o n ' ts e ei t ! { p u b l i cb o o lE x p o r t S e s s i o n s ( s t r i n gs F o r m a t ,S e s s i o n [ ]o S e s s i o n s , D i c t i o n a r y < s t r i n g ,o b j e c t >d i c t O p t i o n s , E v e n t H a n d l e r < P r o g r e s s C a l l b a c k E v e n t A r g s >e v t P r o g r e s s N o t i f i c a t i o n s ) { b o o lb R e s u l t=f a l s e ; s t r i n gc h S p l i t ;
http://fiddlerbook.com/Fiddler/dev/ISessionExport.asp 3/7

3/25/2014

Fiddler Web Debugger - Importing and Exported from Fiddler with .NET Code

/ /D e t e r m i n ei fw ea l r e a d yh a v eaf i l e n a m ef r o mt h ed i c t O p t i o n s c o l l e c t i o n s t r i n gs F i l e n a m e=n u l l ; i f( n u l l! =d i c t O p t i o n s& &d i c t O p t i o n s . C o n t a i n s K e y ( " F i l e n a m e " ) ) { s F i l e n a m e=d i c t O p t i o n s [ " F i l e n a m e " ]a ss t r i n g ; } i f( s F o r m a t= =" C o m m a S e p a r a t e dV a l u e s " ) { c h S p l i t=" , " ; i f( s t r i n g . I s N u l l O r E m p t y ( s F i l e n a m e ) )s F i l e n a m e= F i d d l e r . U t i l i t i e s . O b t a i n S a v e F i l e n a m e ( " E x p o r tA s"+s F o r m a t ," C S VF i l e s ( * . c s v ) | * . c s v " ) ; } e l s e { c h S p l i t=" \ t " ; i f( s t r i n g . I s N u l l O r E m p t y ( s F i l e n a m e ) )s F i l e n a m e= F i d d l e r . U t i l i t i e s . O b t a i n S a v e F i l e n a m e ( " E x p o r tA s"+s F o r m a t ," T S VF i l e s ( * . t s v ) | * . t s v " ) ; } i f( S t r i n g . I s N u l l O r E m p t y ( s F i l e n a m e ) )r e t u r nf a l s e ; t r y { S t r e a m W r i t e rs w O u t p u t=n e wS t r e a m W r i t e r ( s F i l e n a m e ,f a l s e , E n c o d i n g . U T F 8 ) ; i n ti C o u n t=0 ; i n ti M a x=o S e s s i o n s . L e n g t h ; # r e g i o nW r i t e C o l H e a d e r s b o o lb F i r s t C o l=t r u e ; f o r e a c h( C o l u m n H e a d e ro L V C o li n F i d d l e r A p p l i c a t i o n . U I . l v S e s s i o n s . C o l u m n s ) { i f( ! b F i r s t C o l ) { s w O u t p u t . W r i t e ( c h S p l i t ) ; } e l s e { b F i r s t C o l=f a l s e ; } s w O u t p u t . W r i t e ( o L V C o l . T e x t . R e p l a c e ( c h S p l i t ," " ) ) ; } s w O u t p u t . W r i t e L i n e ( ) ; # e n d r e g i o nW r i t e C o l H e a d e r s # r e g i o nW r i t e E a c h S e s s i o n f o r e a c h( S e s s i o no Si no S e s s i o n s ) { i C o u n t + + ; i f( n u l l! =o S . V i e w I t e m ) { b F i r s t C o l=t r u e ;
http://fiddlerbook.com/Fiddler/dev/ISessionExport.asp 4/7

3/25/2014

Fiddler Web Debugger - Importing and Exported from Fiddler with .NET Code

L i s t V i e w I t e mo L V I=( o S . V i e w I t e ma sL i s t V i e w I t e m ) ; i f( n u l l= =o L V I )c o n t i n u e ; f o r e a c h( L i s t V i e w I t e m . L i s t V i e w S u b I t e mo L V Ci no L V I . S u b I t e m s ) { i f( ! b F i r s t C o l ) { s w O u t p u t . W r i t e ( c h S p l i t ) ; } e l s e { b F i r s t C o l=f a l s e ; } s w O u t p u t . W r i t e ( o L V C . T e x t . R e p l a c e ( c h S p l i t ," " ) ) ; } s w O u t p u t . W r i t e L i n e ( ) ; } i f( n u l l! =e v t P r o g r e s s N o t i f i c a t i o n s ) { e v t P r o g r e s s N o t i f i c a t i o n s ( n u l l ,n e wP r o g r e s s C a l l b a c k E v e n t A r g s ( ,) ) ; P r o g r e s s C a l l b a c k E v e n t A r g sP C E A=n e w P r o g r e s s C a l l b a c k E v e n t A r g s ( ( i C o u n t / ( f l o a t ) i M a x ) ," w r o t e"+i C o u n t . T o S t r i n g ( ) +"r e c o r d s . " ) ; e v t P r o g r e s s N o t i f i c a t i o n s ( n u l l ,P C E A ) ; i f( P C E A . C a n c e l ){s w O u t p u t . C l o s e ( ) ;r e t u r nf a l s e ;} } } # e n d r e g i o nW r i t e E a c h S e s s i o n s w O u t p u t . C l o s e ( ) ; b R e s u l t=t r u e ; } c a t c h( E x c e p t i o ne X ) { M e s s a g e B o x . S h o w ( e X . M e s s a g e ," F a i l e dt oe x p o r t " ) ; b R e s u l t=f a l s e ; } } r e t u r nb R e s u l t ; } p u b l i cv o i dD i s p o s e ( ) { } }

Other Examples and Information


Passing arguments to the Transcoder Transcoders may be passed arguments in a dictionary object. For instance, FiddlerScript can invoke the HTTPArchive transcoder, passing in the filename string and maximum response size integers thusly: v a ro S e s s i o n s=F i d d l e r A p p l i c a t i o n . U I . G e t A l l S e s s i o n s ( ) ;
http://fiddlerbook.com/Fiddler/dev/ISessionExport.asp 5/7

3/25/2014

Fiddler Web Debugger - Importing and Exported from Fiddler with .NET Code

v a ro E x p o r t O p t i o n s=F i d d l e r O b j e c t . c r e a t e D i c t i o n a r y ( ) ; o E x p o r t O p t i o n s . A d d ( " F i l e n a m e " ," C : \ \ u s e r s \ \ e r i c l a w \ \ d e s k t o p \ \ o u t 1 . h a r " ) ; o E x p o r t O p t i o n s . A d d ( " M a x T e x t B o d y L e n g t h " ,1 0 2 4 ) ; o E x p o r t O p t i o n s . A d d ( " M a x B i n a r y B o d y L e n g t h " ,1 6 3 8 4 ) ; F i d d l e r A p p l i c a t i o n . D o E x p o r t ( " H T T P A r c h i v ev 1 . 2 " ,o S e s s i o n s ,o E x p o r t O p t i o n s , n u l l ) ; Your Transcoder extension may collect these options like so: p u b l i cb o o lE x p o r t S e s s i o n s ( s t r i n gs F o r m a t ,S e s s i o n [ ]o S e s s i o n s , D i c t i o n a r y < s t r i n g ,o b j e c t >d i c t O p t i o n s , E v e n t H a n d l e r < P r o g r e s s C a l l b a c k E v e n t A r g s >e v t P r o g r e s s N o t i f i c a t i o n s ) { / / . . . i f( n u l l! =d i c t O p t i o n s ) { i f( d i c t O p t i o n s . C o n t a i n s K e y ( " F i l e n a m e " ) ) { s F i l e n a m e=d i c t O p t i o n s [ " F i l e n a m e " ]a ss t r i n g ; } i f( d i c t O p t i o n s . C o n t a i n s K e y ( " M a x T e x t B o d y L e n g t h " ) ) { i M a x T e x t B o d y L e n g t h=( i n t ) d i c t O p t i o n s [ " M a x T e x t B o d y L e n g t h " ] ; } i f( d i c t O p t i o n s . C o n t a i n s K e y ( " M a x B i n a r y B o d y L e n g t h " ) ) { i M a x B i n a r y B o d y L e n g t h=( i n t ) d i c t O p t i o n s [ " M a x B i n a r y B o d y L e n g t h " ] ; } } Almost done... Compile your project. Drop your assembly .DLL in your \Documents\Fiddler2\ImportExport\ folder (or use \Program Files\Fiddler2\ImportExport\to make available to all users on the machine) Restart Fiddler2. Click File > Import or File > Export If you need help or have questions, please email me using the Contact link above.
Change Log 6/21/10 - Created 6/28/10 - Updated with Cancel flag 6/29/10 - Updated Interface signatures with dictOptions collection introduced in Fiddler 2.2.9.7 4/04/11 - Updated with additional information about passing parameters via Options dictionary

2013 Telerik

http://fiddlerbook.com/Fiddler/dev/ISessionExport.asp

6/7

3/25/2014

Fiddler Web Debugger - Importing and Exported from Fiddler with .NET Code

http://fiddlerbook.com/Fiddler/dev/ISessionExport.asp

7/7

Potrebbero piacerti anche