Through the previous blog [small tank: Fiddler tutorial], we learned about the basic usage of Fiddler. Now let's take a look at the advanced usage of Fiddler. Fiddler Script. The script in Fiddler allows us to automatically modify the Http request and Response content. You do not need to manually go to the "breakpoint" to modify the value in the http Request or Response.
Reading directory
- Author of Fiddler
- How to ask the Fiddler author
- About Fiddler Script and Jscript. NET
- Fiddler Script is written in JScript. NET.
- Install Fiddler Script Editor
- Add menu for Fiddler
- Modify the display style of a Session in Fiddler
- How to modify a Cookie in Fiddler Script
- How to modify the body in the Request in Fiddler Script
Author of Fiddler
Eric Lawrence, the author of Fiddler, is a master. He is currently working in Seattle, Microsoft's headquarters. His blog: http://www.ericlawrence.com/Eric/
His resume and some photos of his life can be seen in the blog.
If you have any questions, ask the Fiddler author.
Eric Lawrence built a Fiddler discussion group in the Google forum at: https://groups.google.com/forum? Fromgroups #! Forum/httpfiddler
If you have any questions about Fiddler, you can directly ask Eric Law. Eric Law in this forum to reply to you soon. Search for a question. Because your questions may have been asked long ago.
About Fiddler Script
Fiddler contains a script file that can automatically modify the Http Request and Response. In this way, we do not need to manually modify the "breakpoint,
In fact, it is a script file CustomRules. js
Located in: C: \ Documents and Settings \ [your user] \ My Documents \ Fiddler2 \ Scripts \ CustomRules. js
You can also open the CustomRules. js file in Fiddler, start Fiddler, and click the menu Rules-> Customize Rules...
The official help documentation for Fiddler Script must be carefully read at: http://www.fiddler2.com/Fiddler/dev/ScriptSamples.asp
Fiddler Script is written in JScript. NET.
I have never heard of the JScript. NET language. It may be an upgraded version of JScript. Write a bit like C #
Install Fiddler Script Editor
You can directly use the notepadCustomRules. js file,
It is strongly recommended to download the Fiddler Script Editor address: http://www.fiddler2.com/fiddler/fse.asp
The Fiddler Script Editor provides syntax highlighting and smart prompting functions, such:
Main Methods in CustomRules. js
// Modify the Request content in this method. We use the most,
Static function OnBeforeRequest (oSession: Session)
// Modify the content of Response in this method,
Static function OnBeforeResponse (oSession: Session)
// Include the Fiddler command in each method. On the Fiddler interface, click QuickExec Box at the bottom left.
Static function OnExecAction (sParams: String [])
Add menu for Fiddler
Fiddler can simulate various browsers. You can click the menu Rules-> User-Agents
But it seems that there is no Iphone 4S safari, we add one now. First query the online Iphone 4S safari user-Agents, then add the following code on it
RulesStringValue(23, "Iphone 4S safari", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7")
Save the script and restart Fiddler to see the Iphone 4S in the menu
You can define all common operations as a menu,
Modify the display style of a Session in Fiddler
We can control the style of the Session displayed in Fiddler, the document is: http://www.fiddler2.com/Fiddler/dev/SessionFlags.asp
Place this script in the OnBeforeRequest (oSession: Session) method, and click "Save script". In this way, all cnblogs sessions are displayed in red.
if (oSession.HostnameIs("www.cnblogs.com")) {
oSession["ui-color"] = "red";
}
The running effect is as follows:
How to modify a Cookie in Fiddler Script
Cookie is actually a header in the request.
// Delete all cookies
OSession. oRequest. headers. Remove ("Cookie ");
// Create a cookie
OSession. oRequest. headers. Add ("Cookie", "username = testname; testpassword = P @ ssword1 ");
Note: Fiddler script cannot directly delete or edit a single cookie. You need to use the replace method or regular expression method to operate the string of the cookie.
Static function OnBeforeRequest (oSession: Session) {if (oSession. hostnameIs ('www .example.com ') & oSession. uriContains ('pagewithcooker') & oSession. oRequest. headers. contains ("Cookie") {var sCookie = oSession. oRequest ["Cookie"]; // use the replace method or regular expression to operate string sCookie = sCookie. replace ("cookieName =", "ignoreme ="); oSession. oRequest ["Cookie"] = sCookie ;}
How to modify the body in the Request in Fiddler Script
Method 1:
Static function OnBeforeRequest (oSession: Session) {if (oSession. uriContains ("http://www.cnblogs.com/TankXiao/") {// gets the body string var strBody = oSession in the Request. getRequestBodyAsString (); // use a regular expression or the replace method to modify string strBody = strBody. replace ("1111", "2222"); // check the modified body in a dialog box.
FiddlerObject. alert (strBody); // write the modified body back to oSession. utilSetRequestBody (strBody) in the Request );}}
Method 2: provides a very simple method to directly replace the data in the body.
OSession. utilReplaceInRequest ("1111", "2222 ");