当前位置:   article > 正文

Fiddler 导出请求为curl格式

Fiddler 导出请求为curl格式

来自:https://www.cnblogs.com/yudongdong/p/15418181.html

Fiddler 下载地址:

https://downloads.getfiddler.com/fiddler-classic/FiddlerSetup.5.0.20243.10853-latest.exe

这段代码加到类中

  1. public static RulesOption("关闭请求体转代码", "生成代码")
  2. var m_DisableReuqest: boolean = false;
  3. public static RulesOption("关闭返回体转代码", "生成代码")
  4. var m_DisableResponse: boolean = false;
  5. public static ContextAction("C#-httpclient", "生成代码")
  6. function do1(arrSess: Session[]) { doStar(arrSess, "csharp","httpclient"); }
  7. public static ContextAction("C#-restsharp", "生成代码")
  8. function do2(arrSess: Session[]) { doStar(arrSess, "csharp","restsharp"); }
  9. public static ContextAction("Java-okhttp", "生成代码")
  10. function do3(arrSess: Session[]) { doStar(arrSess, "java","okhttp"); }
  11. public static ContextAction("Java-asynchttp", "生成代码")
  12. function do4(arrSess: Session[]) { doStar(arrSess, "java","asynchttp"); }
  13. public static ContextAction("Java-nethttp", "生成代码")
  14. function do5(arrSess: Session[]) { doStar(arrSess, "java","nethttp"); }
  15. public static ContextAction("Java-unirest", "生成代码")
  16. function do6(arrSess: Session[]) { doStar(arrSess, "java","unirest"); }
  17. public static ContextAction("Kotlin-okhttp", "生成代码")
  18. function do7(arrSess: Session[]) { doStar(arrSess, "kotlin","okhttp"); }
  19. public static ContextAction("JavaScript-xhr", "生成代码")
  20. function do8(arrSess: Session[]) { doStar(arrSess, "javascript","xhr"); }
  21. public static ContextAction("JavaScript-jquery", "生成代码")
  22. function do9(arrSess: Session[]) { doStar(arrSess, "javascript","jquery"); }
  23. public static ContextAction("JavaScript-fetch", "生成代码")
  24. function do10(arrSess: Session[]) { doStar(arrSess, "javascript","fetch"); }
  25. public static ContextAction("JavaScript-axios", "生成代码")
  26. function do11(arrSess: Session[]) { doStar(arrSess, "javascript","axios"); }
  27. public static ContextAction("Node-native", "生成代码")
  28. function do12(arrSess: Session[]) { doStar(arrSess, "node","native"); }
  29. public static ContextAction("Node-request", "生成代码")
  30. function do13(arrSess: Session[]) { doStar(arrSess, "node","request"); }
  31. public static ContextAction("Node-fetch", "生成代码")
  32. function do14(arrSess: Session[]) { doStar(arrSess, "node","fetch"); }
  33. public static ContextAction("Node-axios", "生成代码")
  34. function do15(arrSess: Session[]) { doStar(arrSess, "node","axios"); }
  35. public static ContextAction("Node-unirest", "生成代码")
  36. function do16(arrSess: Session[]) { doStar(arrSess, "node","unirest"); }
  37. public static ContextAction("Python3-http.client", "生成代码")
  38. function do17(arrSess: Session[]) { doStar(arrSess, "python","python3"); }
  39. public static ContextAction("Python-requests", "生成代码")
  40. function do18(arrSess: Session[]) { doStar(arrSess, "python","requests"); }
  41. public static ContextAction("ObjectiveC-nsurlsession", "生成代码")
  42. function do19(arrSess: Session[]) { doStar(arrSess, "objc","nsurlsession"); }
  43. public static ContextAction("Ruby-net::http", "生成代码")
  44. function do20(arrSess: Session[]) { doStar(arrSess, "ruby","native"); }
  45. public static ContextAction("Swift-nsurlsession", "生成代码")
  46. function do21(arrSess: Session[]) { doStar(arrSess, "swift","nsurlsession"); }
  47. public static ContextAction("powershell-webrequest", "生成代码")
  48. function do22(arrSess: Session[]) { doStar(arrSess, "powershell","webrequest"); }
  49. public static ContextAction("powershell-restmethod", "生成代码")
  50. function do23(arrSess: Session[]) { doStar(arrSess, "powershell","restmethod"); }
  51. public static ContextAction("Shell-curl", "生成代码")
  52. function do24(arrSess: Session[]) { doStar(arrSess, "shell","curl"); }
  53. public static ContextAction("Shell-httpie", "生成代码")
  54. function do25(arrSess: Session[]) { doStar(arrSess, "shell","httpie"); }
  55. public static ContextAction("Shell-wget", "生成代码")
  56. function do26(arrSess: Session[]) { doStar(arrSess, "shell","wget"); }
  57. public static ContextAction("Go-NewRequest", "生成代码")
  58. function do27(arrSess: Session[]) { doStar(arrSess, "go","native"); }
  59. public static ContextAction("Clojure-clj_http", "生成代码")
  60. function do28(arrSess: Session[]) { doStar(arrSess, "clojure","clj_http"); }
  61. public static ContextAction("C-Libcurl", "生成代码")
  62. function do29(arrSess: Session[]) { doStar(arrSess, "c","libcurl"); }
  63. public static ContextAction("PHP-curl", "生成代码")
  64. function do30(arrSess: Session[]) { doStar(arrSess, "php","curl"); }
  65. public static ContextAction("PHP-http1", "生成代码")
  66. function do31(arrSess: Session[]) { doStar(arrSess, "php","http1"); }
  67. public static ContextAction("PHP-http2", "生成代码")
  68. function do32(arrSess: Session[]) { doStar(arrSess, "php","http2"); }
  69. public static function doStar(oSessions: Session[], target: String,client:String) {
  70. //注意看这里,请下载我给的这2个exe并替换成你电脑中正确的目录
  71. var httpsnippet = "E:\\fiddler生成代码\\httpsnippet.exe";
  72. var quicktype = "E:\\fiddler生成代码\\quicktype.exe";
  73. var oExportOptions = FiddlerObject.createDictionary();
  74. var tempPath2 = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler.har");
  75. if(System.IO.File.Exists(tempPath2)){
  76. System.IO.File.Delete(tempPath2);
  77. }
  78. var tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler.json");
  79. if(System.IO.File.Exists(tempPath)){
  80. System.IO.File.Delete(tempPath);
  81. }
  82. var tempRequestBodyPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_requestBody.json");
  83. if(System.IO.File.Exists(tempRequestBodyPath)){
  84. System.IO.File.Delete(tempRequestBodyPath);
  85. }
  86. var tempResponseBodyPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_responseBody.json");
  87. if(System.IO.File.Exists(tempResponseBodyPath)){
  88. System.IO.File.Delete(tempResponseBodyPath);
  89. }
  90. oExportOptions.Add("Filename", tempPath2);
  91. FiddlerApplication.DoExport("HTTPArchive v1.2", oSessions,oExportOptions, null);
  92. System.IO.File.Move(tempPath2, tempPath);
  93. if(!System.IO.File.Exists(tempPath)){
  94. MessageBox.Show("生成代码失败", "No action");
  95. return;
  96. }
  97. var rtPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_rt");
  98. if(System.IO.Directory.Exists(rtPath))System.IO.Directory.Delete(rtPath,true);
  99. if(!doProcess(httpsnippet, "\""+tempPath+"\" -t "+target+" -c "+client+" -o " + "\""+rtPath+"\"")){
  100. MessageBox.Show("生成代码错误", "No action");
  101. return;
  102. }
  103. var file = System.IO.Directory.GetFiles(rtPath);
  104. if(file.Length!=1){
  105. MessageBox.Show("生成代码错误", "No action");
  106. return;
  107. }
  108. var json = System.IO.File.ReadAllText(file[0]);
  109. System.IO.File.Delete(file[0]);
  110. var rtPath1 = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_request_body");
  111. if(System.IO.File.Exists(rtPath1))System.IO.File.Delete(rtPath1);
  112. if(!m_DisableReuqest && System.IO.File.Exists(tempRequestBodyPath)){
  113. json += getJsonCode(quicktype,tempRequestBodyPath,rtPath,rtPath1,target,"FiddlerRequest");
  114. }
  115. rtPath1 = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_response_body");
  116. if(System.IO.File.Exists(rtPath1))System.IO.File.Delete(rtPath1);
  117. if(!m_DisableResponse && System.IO.File.Exists(tempResponseBodyPath)){
  118. json += getJsonCode(quicktype,tempResponseBodyPath,rtPath,rtPath1,target, "FiddlerReponse");
  119. }
  120. Clipboard.SetText(json);
  121. MessageBox.Show("代码生成成功,已复制到剪贴板");
  122. }
  123. static function getJsonCode(file: String,tempRequestBodyPath:String,rtPath:String,rtPath1:String,target:String,type:String): String {
  124. var json = "";
  125. var tmp1 = "";
  126. if(target == 'csharp'){
  127. tmp1 = "--quiet --telemetry disable --features just-types --array-type list --no-check-required --namespace \"Fiddlers\" --lang \"" + target + "\" --top-level \""+type+"Model\" \"" + tempRequestBodyPath + "\"" +" -o " + "\""+rtPath1+"\"";
  128. }
  129. else if(target == 'kotlin'){
  130. tmp1 = "--quiet --telemetry disable --framework just-types --lang \"" + target + "\" --top-level \""+type+"Model\" \"" + tempRequestBodyPath + "\"" +" -o " + "\""+rtPath1+"\"";
  131. }
  132. else if(target == 'java'){
  133. tmp1 = "--quiet --telemetry disable --array-type list --just-types --package \"Fiddlers\" --lang \"" + target + "\" --top-level \""+type+"Model\" \"" + tempRequestBodyPath + "\"" +" -o " + "\""+rtPath+"\\test"+"\"";
  134. }
  135. else {
  136. tmp1 = "--telemetry disable --just-types --lang \"" + target + "\" --top-level \""+type+"Models\" \"" + tempRequestBodyPath + "\"" +" -o " + "\""+rtPath1+"\"";
  137. }
  138. doProcess(file, tmp1)
  139. if(System.IO.File.Exists(rtPath1)){
  140. json += "\r\n//"+type+"-POJO\r\n" + System.IO.File.ReadAllText(rtPath1).Replace("package quicktype","");
  141. }
  142. if(target == 'java'){
  143. var javaFiles = System.IO.Directory.GetFiles(rtPath,"*.java");
  144. if(javaFiles.Length>0){
  145. json += "\r\n//"+type+"-POJO\r\n" ;
  146. for (var i:int = 0; i<javaFiles.Length; i++)
  147. {
  148. json += System.IO.File.ReadAllText(javaFiles[i]).Replace("package Fiddlers;","")
  149. System.IO.File.Delete(javaFiles[i]);
  150. }
  151. }
  152. }
  153. return json;
  154. }
  155. static function doProcess(file: String,paramsList:String): Boolean {
  156. var process = new System.Diagnostics.Process();
  157. process.StartInfo.FileName = file;
  158. process.StartInfo.Arguments = paramsList;
  159. process.StartInfo.CreateNoWindow = true;
  160. process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  161. process.StartInfo.UseShellExecute = false;
  162. process.StartInfo.Verb = "runas";
  163. process.StartInfo.RedirectStandardError = true;
  164. process.StartInfo.RedirectStandardOutput = true;
  165. process.Start();
  166. process.WaitForExit();
  167. process.Dispose();
  168. return true;
  169. }

整体代码

  1. import System;
  2. import System.Windows.Forms;
  3. import Fiddler;
  4. // INTRODUCTION
  5. //
  6. // Well, hello there!
  7. //
  8. // Don't be scared! :-)
  9. //
  10. // This is the FiddlerScript Rules file, which creates some of the menu commands and
  11. // other features of Progress Telerik Fiddler Classic. You can edit this file to modify or add new commands.
  12. //
  13. // The original version of this file is named SampleRules.js and it is in the
  14. // \Program Files\Fiddler\ folder. When Fiddler Classic first runs, it creates a copy named
  15. // CustomRules.js inside your \Documents\Fiddler2\Scripts folder. If you make a
  16. // mistake in editing this file, simply delete the CustomRules.js file and restart
  17. // Fiddler Classic. A fresh copy of the default rules will be created from the original
  18. // sample rules file.
  19. // The best way to edit this file is to install the FiddlerScript Editor, part of
  20. // the free SyntaxEditing addons. Get it here: http://fiddler2.com/r/?SYNTAXVIEWINSTALL
  21. // GLOBALIZATION NOTE: Save this file using UTF-8 Encoding.
  22. // JScript.NET Reference
  23. // http://fiddler2.com/r/?msdnjsnet
  24. //
  25. // FiddlerScript Reference
  26. // http://fiddler2.com/r/?fiddlerscriptcookbook
  27. class Handlers
  28. {
  29. // *****************
  30. //
  31. // This is the Handlers class. Pretty much everything you ever add to FiddlerScript
  32. // belongs right inside here, or inside one of the already-existing functions below.
  33. //
  34. // *****************
  35. // The following snippet demonstrates a custom-bound column for the Web Sessions list.
  36. // See http://fiddler2.com/r/?fiddlercolumns for more info
  37. /*
  38. public static BindUIColumn("Method", 60)
  39. function FillMethodColumn(oS: Session): String {
  40. return oS.RequestMethod;
  41. }
  42. */
  43. // The following snippet demonstrates how to create a custom tab that shows simple text
  44. /*
  45. public BindUITab("Flags")
  46. static function FlagsReport(arrSess: Session[]):String {
  47. var oSB: System.Text.StringBuilder = new System.Text.StringBuilder();
  48. for (var i:int = 0; i<arrSess.Length; i++)
  49. {
  50. oSB.AppendLine("SESSION FLAGS");
  51. oSB.AppendFormat("{0}: {1}\n", arrSess[i].id, arrSess[i].fullUrl);
  52. for(var sFlag in arrSess[i].oFlags)
  53. {
  54. oSB.AppendFormat("\t{0}:\t\t{1}\n", sFlag.Key, sFlag.Value);
  55. }
  56. }
  57. return oSB.ToString();
  58. }
  59. */
  60. // You can create a custom menu like so:
  61. /*
  62. QuickLinkMenu("&Links")
  63. QuickLinkItem("IE GeoLoc TestDrive", "http://ie.microsoft.com/testdrive/HTML5/Geolocation/Default.html")
  64. QuickLinkItem("FiddlerCore", "http://fiddler2.com/fiddlercore")
  65. public static function DoLinksMenu(sText: String, sAction: String)
  66. {
  67. Utilities.LaunchHyperlink(sAction);
  68. }
  69. */
  70. public static RulesOption("Hide 304s")
  71. BindPref("fiddlerscript.rules.Hide304s")
  72. var m_Hide304s: boolean = false;
  73. // Cause Fiddler Classic to override the Accept-Language header with one of the defined values
  74. public static RulesOption("Request &Japanese Content")
  75. var m_Japanese: boolean = false;
  76. // Automatic Authentication
  77. public static RulesOption("&Automatically Authenticate")
  78. BindPref("fiddlerscript.rules.AutoAuth")
  79. var m_AutoAuth: boolean = false;
  80. // Cause Fiddler Classic to override the User-Agent header with one of the defined values
  81. // The page http://browserscope2.org/browse?category=selectors&ua=Mobile%20Safari is a good place to find updated versions of these
  82. RulesString("&User-Agents", true)
  83. BindPref("fiddlerscript.ephemeral.UserAgentString")
  84. RulesStringValue(0,"Netscape &3", "Mozilla/3.0 (Win95; I)")
  85. RulesStringValue(1,"WinPhone8.1", "Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 520) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537")
  86. RulesStringValue(2,"&Safari5 (Win7)", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1")
  87. RulesStringValue(3,"Safari9 (Mac)", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56")
  88. RulesStringValue(4,"iPad", "Mozilla/5.0 (iPad; CPU OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F5027d Safari/600.1.4")
  89. RulesStringValue(5,"iPhone6", "Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4")
  90. RulesStringValue(6,"IE &6 (XPSP2)", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")
  91. RulesStringValue(7,"IE &7 (Vista)", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1)")
  92. RulesStringValue(8,"IE 8 (Win2k3 x64)", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; WOW64; Trident/4.0)")
  93. RulesStringValue(9,"IE &8 (Win7)", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)")
  94. RulesStringValue(10,"IE 9 (Win7)", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
  95. RulesStringValue(11,"IE 10 (Win8)", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)")
  96. RulesStringValue(12,"IE 11 (Surface2)", "Mozilla/5.0 (Windows NT 6.3; ARM; Trident/7.0; Touch; rv:11.0) like Gecko")
  97. RulesStringValue(13,"IE 11 (Win8.1)", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko")
  98. RulesStringValue(14,"Edge (Win10)", "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.11082")
  99. RulesStringValue(15,"&Opera", "Opera/9.80 (Windows NT 6.2; WOW64) Presto/2.12.388 Version/12.17")
  100. RulesStringValue(16,"&Firefox 3.6", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.7) Gecko/20100625 Firefox/3.6.7")
  101. RulesStringValue(17,"&Firefox 43", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0")
  102. RulesStringValue(18,"&Firefox Phone", "Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0")
  103. RulesStringValue(19,"&Firefox (Mac)", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0")
  104. RulesStringValue(20,"Chrome (Win)", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.48 Safari/537.36")
  105. RulesStringValue(21,"Chrome (Android)", "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.78 Mobile Safari/537.36")
  106. RulesStringValue(22,"ChromeBook", "Mozilla/5.0 (X11; CrOS x86_64 6680.52.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.74 Safari/537.36")
  107. RulesStringValue(23,"GoogleBot Crawler", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
  108. RulesStringValue(24,"Kindle Fire (Silk)", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.0.22.79_10013310) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true")
  109. RulesStringValue(25,"&Custom...", "%CUSTOM%")
  110. public static var sUA: String = null;
  111. // Cause Fiddler Classic to delay HTTP traffic to simulate typical 56k modem conditions
  112. public static RulesOption("Simulate &Modem Speeds", "Per&formance")
  113. var m_SimulateModem: boolean = false;
  114. // Removes HTTP-caching related headers and specifies "no-cache" on requests and responses
  115. public static RulesOption("&Disable Caching", "Per&formance")
  116. var m_DisableCaching: boolean = false;
  117. public static RulesOption("Cache Always &Fresh", "Per&formance")
  118. var m_AlwaysFresh: boolean = false;
  119. // Force a manual reload of the script file. Resets all
  120. // RulesOption variables to their defaults.
  121. public static ToolsAction("Reset Script")
  122. function DoManualReload() {
  123. FiddlerObject.ReloadScript();
  124. }
  125. public static ContextAction("Decode Selected Sessions")
  126. function DoRemoveEncoding(oSessions: Session[]) {
  127. for (var x:int = 0; x < oSessions.Length; x++){
  128. oSessions[x].utilDecodeRequest();
  129. oSessions[x].utilDecodeResponse();
  130. }
  131. UI.actUpdateInspector(true,true);
  132. }
  133. static function OnBeforeRequest(oSession: Session) {
  134. // Sample Rule: Color ASPX requests in RED
  135. // if (oSession.uriContains(".aspx")) { oSession["ui-color"] = "red"; }
  136. // Sample Rule: Flag POSTs to fiddler2.com in italics
  137. // if (oSession.HostnameIs("www.fiddler2.com") && oSession.HTTPMethodIs("POST")) { oSession["ui-italic"] = "yup"; }
  138. // Sample Rule: Break requests for URLs containing "/sandbox/"
  139. // if (oSession.uriContains("/sandbox/")) {
  140. // oSession.oFlags["x-breakrequest"] = "yup"; // Existence of the x-breakrequest flag creates a breakpoint; the "yup" value is unimportant.
  141. // }
  142. if ((null != gs_ReplaceToken) && (oSession.url.indexOf(gs_ReplaceToken)>-1)) { // Case sensitive
  143. oSession.url = oSession.url.Replace(gs_ReplaceToken, gs_ReplaceTokenWith);
  144. }
  145. if ((null != gs_OverridenHost) && (oSession.host.toLowerCase() == gs_OverridenHost)) {
  146. oSession["x-overridehost"] = gs_OverrideHostWith;
  147. }
  148. if ((null!=bpRequestURI) && oSession.uriContains(bpRequestURI)) {
  149. oSession["x-breakrequest"]="uri";
  150. }
  151. if ((null!=bpMethod) && (oSession.HTTPMethodIs(bpMethod))) {
  152. oSession["x-breakrequest"]="method";
  153. }
  154. if ((null!=uiBoldURI) && oSession.uriContains(uiBoldURI)) {
  155. oSession["ui-bold"]="QuickExec";
  156. }
  157. if (m_SimulateModem) {
  158. // Delay sends by 300ms per KB uploaded.
  159. oSession["request-trickle-delay"] = "300";
  160. // Delay receives by 150ms per KB downloaded.
  161. oSession["response-trickle-delay"] = "150";
  162. }
  163. if (m_DisableCaching) {
  164. oSession.oRequest.headers.Remove("If-None-Match");
  165. oSession.oRequest.headers.Remove("If-Modified-Since");
  166. oSession.oRequest["Pragma"] = "no-cache";
  167. }
  168. // User-Agent Overrides
  169. if (null != sUA) {
  170. oSession.oRequest["User-Agent"] = sUA;
  171. }
  172. if (m_Japanese) {
  173. oSession.oRequest["Accept-Language"] = "ja";
  174. }
  175. if (m_AutoAuth) {
  176. // Automatically respond to any authentication challenges using the
  177. // current Fiddler Classic user's credentials. You can change (default)
  178. // to a domain\\username:password string if preferred.
  179. //
  180. // WARNING: This setting poses a security risk if remote
  181. // connections are permitted!
  182. oSession["X-AutoAuth"] = "(default)";
  183. }
  184. if (m_AlwaysFresh && (oSession.oRequest.headers.Exists("If-Modified-Since") || oSession.oRequest.headers.Exists("If-None-Match")))
  185. {
  186. oSession.utilCreateResponseAndBypassServer();
  187. oSession.responseCode = 304;
  188. oSession["ui-backcolor"] = "Lavender";
  189. }
  190. }
  191. // This function is called immediately after a set of request headers has
  192. // been read from the client. This is typically too early to do much useful
  193. // work, since the body hasn't yet been read, but sometimes it may be useful.
  194. //
  195. // For instance, see
  196. // http://blogs.msdn.com/b/fiddler/archive/2011/11/05/http-expect-continue-delays-transmitting-post-bodies-by-up-to-350-milliseconds.aspx
  197. // for one useful thing you can do with this handler.
  198. //
  199. // Note: oSession.requestBodyBytes is not available within this function!
  200. /*
  201. static function OnPeekAtRequestHeaders(oSession: Session) {
  202. var sProc = ("" + oSession["x-ProcessInfo"]).ToLower();
  203. if (!sProc.StartsWith("mylowercaseappname")) oSession["ui-hide"] = "NotMyApp";
  204. }
  205. */
  206. //
  207. // If a given session has response streaming enabled, then the OnBeforeResponse function
  208. // is actually called AFTER the response was returned to the client.
  209. //
  210. // In contrast, this OnPeekAtResponseHeaders function is called before the response headers are
  211. // sent to the client (and before the body is read from the server). Hence this is an opportune time
  212. // to disable streaming (oSession.bBufferResponse = true) if there is something in the response headers
  213. // which suggests that tampering with the response body is necessary.
  214. //
  215. // Note: oSession.responseBodyBytes is not available within this function!
  216. //
  217. static function OnPeekAtResponseHeaders(oSession: Session) {
  218. //FiddlerApplication.Log.LogFormat("Session {0}: Response header peek shows status is {1}", oSession.id, oSession.responseCode);
  219. if (m_DisableCaching) {
  220. oSession.oResponse.headers.Remove("Expires");
  221. oSession.oResponse["Cache-Control"] = "no-cache";
  222. }
  223. if ((bpStatus>0) && (oSession.responseCode == bpStatus)) {
  224. oSession["x-breakresponse"]="status";
  225. oSession.bBufferResponse = true;
  226. }
  227. if ((null!=bpResponseURI) && oSession.uriContains(bpResponseURI)) {
  228. oSession["x-breakresponse"]="uri";
  229. oSession.bBufferResponse = true;
  230. }
  231. }
  232. static function OnBeforeResponse(oSession: Session) {
  233. if (m_Hide304s && oSession.responseCode == 304) {
  234. oSession["ui-hide"] = "true";
  235. }
  236. }
  237. /*
  238. // This function executes just before Fiddler Classic returns an error that it has
  239. // itself generated (e.g. "DNS Lookup failure") to the client application.
  240. // These responses will not run through the OnBeforeResponse function above.
  241. static function OnReturningError(oSession: Session) {
  242. }
  243. */
  244. /*
  245. // This function executes after Fiddler Classic finishes processing a Session, regardless
  246. // of whether it succeeded or failed. Note that this typically runs AFTER the last
  247. // update of the Web Sessions UI listitem, so you must manually refresh the Session's
  248. // UI if you intend to change it.
  249. static function OnDone(oSession: Session) {
  250. }
  251. */
  252. /*
  253. static function OnBoot() {
  254. MessageBox.Show("Fiddler Classic has finished booting");
  255. System.Diagnostics.Process.Start("iexplore.exe");
  256. UI.ActivateRequestInspector("HEADERS");
  257. UI.ActivateResponseInspector("HEADERS");
  258. }
  259. */
  260. /*
  261. static function OnBeforeShutdown(): Boolean {
  262. // Return false to cancel shutdown.
  263. return ((0 == FiddlerApplication.UI.lvSessions.TotalItemCount()) ||
  264. (DialogResult.Yes == MessageBox.Show("Allow Fiddler Classic to exit?", "Go Bye-bye?",
  265. MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)));
  266. }
  267. */
  268. /*
  269. static function OnShutdown() {
  270. MessageBox.Show("Fiddler Classic has shutdown");
  271. }
  272. */
  273. /*
  274. static function OnAttach() {
  275. MessageBox.Show("Fiddler Classic is now the system proxy");
  276. }
  277. */
  278. /*
  279. static function OnDetach() {
  280. MessageBox.Show("Fiddler Classic is no longer the system proxy");
  281. }
  282. */
  283. // The Main() function runs everytime your FiddlerScript compiles
  284. static function Main() {
  285. var today: Date = new Date();
  286. FiddlerObject.StatusText = " CustomRules.js was loaded at: " + today;
  287. // Uncomment to add a "Server" column containing the response "Server" header, if present
  288. // UI.lvSessions.AddBoundColumn("Server", 50, "@response.server");
  289. // Uncomment to add a global hotkey (Win+G) that invokes the ExecAction method below...
  290. // UI.RegisterCustomHotkey(HotkeyModifiers.Windows, Keys.G, "screenshot");
  291. }
  292. // These static variables are used for simple breakpointing & other QuickExec rules
  293. BindPref("fiddlerscript.ephemeral.bpRequestURI")
  294. public static var bpRequestURI:String = null;
  295. BindPref("fiddlerscript.ephemeral.bpResponseURI")
  296. public static var bpResponseURI:String = null;
  297. BindPref("fiddlerscript.ephemeral.bpMethod")
  298. public static var bpMethod: String = null;
  299. static var bpStatus:int = -1;
  300. static var uiBoldURI: String = null;
  301. static var gs_ReplaceToken: String = null;
  302. static var gs_ReplaceTokenWith: String = null;
  303. static var gs_OverridenHost: String = null;
  304. static var gs_OverrideHostWith: String = null;
  305. // The OnExecAction function is called by either the QuickExec box in the Fiddler Classic window,
  306. // or by the ExecAction.exe command line utility.
  307. static function OnExecAction(sParams: String[]): Boolean {
  308. FiddlerObject.StatusText = "ExecAction: " + sParams[0];
  309. var sAction = sParams[0].toLowerCase();
  310. switch (sAction) {
  311. case "bold":
  312. if (sParams.Length<2) {uiBoldURI=null; FiddlerObject.StatusText="Bolding cleared"; return false;}
  313. uiBoldURI = sParams[1]; FiddlerObject.StatusText="Bolding requests for " + uiBoldURI;
  314. return true;
  315. case "bp":
  316. FiddlerObject.alert("bpu = breakpoint request for uri\nbpm = breakpoint request method\nbps=breakpoint response status\nbpafter = breakpoint response for URI");
  317. return true;
  318. case "bps":
  319. if (sParams.Length<2) {bpStatus=-1; FiddlerObject.StatusText="Response Status breakpoint cleared"; return false;}
  320. bpStatus = parseInt(sParams[1]); FiddlerObject.StatusText="Response status breakpoint for " + sParams[1];
  321. return true;
  322. case "bpv":
  323. case "bpm":
  324. if (sParams.Length<2) {bpMethod=null; FiddlerObject.StatusText="Request Method breakpoint cleared"; return false;}
  325. bpMethod = sParams[1].toUpperCase(); FiddlerObject.StatusText="Request Method breakpoint for " + bpMethod;
  326. return true;
  327. case "bpu":
  328. if (sParams.Length<2) {bpRequestURI=null; FiddlerObject.StatusText="RequestURI breakpoint cleared"; return false;}
  329. bpRequestURI = sParams[1];
  330. FiddlerObject.StatusText="RequestURI breakpoint for "+sParams[1];
  331. return true;
  332. case "bpa":
  333. case "bpafter":
  334. if (sParams.Length<2) {bpResponseURI=null; FiddlerObject.StatusText="ResponseURI breakpoint cleared"; return false;}
  335. bpResponseURI = sParams[1];
  336. FiddlerObject.StatusText="ResponseURI breakpoint for "+sParams[1];
  337. return true;
  338. case "overridehost":
  339. if (sParams.Length<3) {gs_OverridenHost=null; FiddlerObject.StatusText="Host Override cleared"; return false;}
  340. gs_OverridenHost = sParams[1].toLowerCase();
  341. gs_OverrideHostWith = sParams[2];
  342. FiddlerObject.StatusText="Connecting to [" + gs_OverrideHostWith + "] for requests to [" + gs_OverridenHost + "]";
  343. return true;
  344. case "urlreplace":
  345. if (sParams.Length<3) {gs_ReplaceToken=null; FiddlerObject.StatusText="URL Replacement cleared"; return false;}
  346. gs_ReplaceToken = sParams[1];
  347. gs_ReplaceTokenWith = sParams[2].Replace(" ", "%20"); // Simple helper
  348. FiddlerObject.StatusText="Replacing [" + gs_ReplaceToken + "] in URIs with [" + gs_ReplaceTokenWith + "]";
  349. return true;
  350. case "allbut":
  351. case "keeponly":
  352. if (sParams.Length<2) { FiddlerObject.StatusText="Please specify Content-Type to retain during wipe."; return false;}
  353. UI.actSelectSessionsWithResponseHeaderValue("Content-Type", sParams[1]);
  354. UI.actRemoveUnselectedSessions();
  355. UI.lvSessions.SelectedItems.Clear();
  356. FiddlerObject.StatusText="Removed all but Content-Type: " + sParams[1];
  357. return true;
  358. case "stop":
  359. UI.actDetachProxy();
  360. return true;
  361. case "start":
  362. UI.actAttachProxy();
  363. return true;
  364. case "cls":
  365. case "clear":
  366. UI.actRemoveAllSessions();
  367. return true;
  368. case "g":
  369. case "go":
  370. UI.actResumeAllSessions();
  371. return true;
  372. case "goto":
  373. if (sParams.Length != 2) return false;
  374. Utilities.LaunchHyperlink("http://www.google.com/search?hl=en&btnI=I%27m+Feeling+Lucky&q=" + Utilities.UrlEncode(sParams[1]));
  375. return true;
  376. case "help":
  377. Utilities.LaunchHyperlink("http://fiddler2.com/r/?quickexec");
  378. return true;
  379. case "hide":
  380. UI.actMinimizeToTray();
  381. return true;
  382. case "log":
  383. FiddlerApplication.Log.LogString((sParams.Length<2) ? "User couldn't think of anything to say..." : sParams[1]);
  384. return true;
  385. case "nuke":
  386. UI.actClearWinINETCache();
  387. UI.actClearWinINETCookies();
  388. return true;
  389. case "screenshot":
  390. UI.actCaptureScreenshot(false);
  391. return true;
  392. case "show":
  393. UI.actRestoreWindow();
  394. return true;
  395. case "tail":
  396. if (sParams.Length<2) { FiddlerObject.StatusText="Please specify # of sessions to trim the session list to."; return false;}
  397. UI.TrimSessionList(int.Parse(sParams[1]));
  398. return true;
  399. case "quit":
  400. UI.actExit();
  401. return true;
  402. case "dump":
  403. UI.actSelectAll();
  404. UI.actSaveSessionsToZip(CONFIG.GetPath("Captures") + "dump.saz");
  405. UI.actRemoveAllSessions();
  406. FiddlerObject.StatusText = "Dumped all sessions to " + CONFIG.GetPath("Captures") + "dump.saz";
  407. return true;
  408. default:
  409. if (sAction.StartsWith("http") || sAction.StartsWith("www.")) {
  410. System.Diagnostics.Process.Start(sParams[0]);
  411. return true;
  412. }
  413. else
  414. {
  415. FiddlerObject.StatusText = "Requested ExecAction: '" + sAction + "' not found. Type HELP to learn more.";
  416. return false;
  417. }
  418. }
  419. }
  420. public static RulesOption("关闭请求体转代码", "生成代码")
  421. var m_DisableReuqest: boolean = false;
  422. public static RulesOption("关闭返回体转代码", "生成代码")
  423. var m_DisableResponse: boolean = false;
  424. public static ContextAction("C#-httpclient", "生成代码")
  425. function do1(arrSess: Session[]) { doStar(arrSess, "csharp","httpclient"); }
  426. public static ContextAction("C#-restsharp", "生成代码")
  427. function do2(arrSess: Session[]) { doStar(arrSess, "csharp","restsharp"); }
  428. public static ContextAction("Java-okhttp", "生成代码")
  429. function do3(arrSess: Session[]) { doStar(arrSess, "java","okhttp"); }
  430. public static ContextAction("Java-asynchttp", "生成代码")
  431. function do4(arrSess: Session[]) { doStar(arrSess, "java","asynchttp"); }
  432. public static ContextAction("Java-nethttp", "生成代码")
  433. function do5(arrSess: Session[]) { doStar(arrSess, "java","nethttp"); }
  434. public static ContextAction("Java-unirest", "生成代码")
  435. function do6(arrSess: Session[]) { doStar(arrSess, "java","unirest"); }
  436. public static ContextAction("Kotlin-okhttp", "生成代码")
  437. function do7(arrSess: Session[]) { doStar(arrSess, "kotlin","okhttp"); }
  438. public static ContextAction("JavaScript-xhr", "生成代码")
  439. function do8(arrSess: Session[]) { doStar(arrSess, "javascript","xhr"); }
  440. public static ContextAction("JavaScript-jquery", "生成代码")
  441. function do9(arrSess: Session[]) { doStar(arrSess, "javascript","jquery"); }
  442. public static ContextAction("JavaScript-fetch", "生成代码")
  443. function do10(arrSess: Session[]) { doStar(arrSess, "javascript","fetch"); }
  444. public static ContextAction("JavaScript-axios", "生成代码")
  445. function do11(arrSess: Session[]) { doStar(arrSess, "javascript","axios"); }
  446. public static ContextAction("Node-native", "生成代码")
  447. function do12(arrSess: Session[]) { doStar(arrSess, "node","native"); }
  448. public static ContextAction("Node-request", "生成代码")
  449. function do13(arrSess: Session[]) { doStar(arrSess, "node","request"); }
  450. public static ContextAction("Node-fetch", "生成代码")
  451. function do14(arrSess: Session[]) { doStar(arrSess, "node","fetch"); }
  452. public static ContextAction("Node-axios", "生成代码")
  453. function do15(arrSess: Session[]) { doStar(arrSess, "node","axios"); }
  454. public static ContextAction("Node-unirest", "生成代码")
  455. function do16(arrSess: Session[]) { doStar(arrSess, "node","unirest"); }
  456. public static ContextAction("Python3-http.client", "生成代码")
  457. function do17(arrSess: Session[]) { doStar(arrSess, "python","python3"); }
  458. public static ContextAction("Python-requests", "生成代码")
  459. function do18(arrSess: Session[]) { doStar(arrSess, "python","requests"); }
  460. public static ContextAction("ObjectiveC-nsurlsession", "生成代码")
  461. function do19(arrSess: Session[]) { doStar(arrSess, "objc","nsurlsession"); }
  462. public static ContextAction("Ruby-net::http", "生成代码")
  463. function do20(arrSess: Session[]) { doStar(arrSess, "ruby","native"); }
  464. public static ContextAction("Swift-nsurlsession", "生成代码")
  465. function do21(arrSess: Session[]) { doStar(arrSess, "swift","nsurlsession"); }
  466. public static ContextAction("powershell-webrequest", "生成代码")
  467. function do22(arrSess: Session[]) { doStar(arrSess, "powershell","webrequest"); }
  468. public static ContextAction("powershell-restmethod", "生成代码")
  469. function do23(arrSess: Session[]) { doStar(arrSess, "powershell","restmethod"); }
  470. public static ContextAction("Shell-curl", "生成代码")
  471. function do24(arrSess: Session[]) { doStar(arrSess, "shell","curl"); }
  472. public static ContextAction("Shell-httpie", "生成代码")
  473. function do25(arrSess: Session[]) { doStar(arrSess, "shell","httpie"); }
  474. public static ContextAction("Shell-wget", "生成代码")
  475. function do26(arrSess: Session[]) { doStar(arrSess, "shell","wget"); }
  476. public static ContextAction("Go-NewRequest", "生成代码")
  477. function do27(arrSess: Session[]) { doStar(arrSess, "go","native"); }
  478. public static ContextAction("Clojure-clj_http", "生成代码")
  479. function do28(arrSess: Session[]) { doStar(arrSess, "clojure","clj_http"); }
  480. public static ContextAction("C-Libcurl", "生成代码")
  481. function do29(arrSess: Session[]) { doStar(arrSess, "c","libcurl"); }
  482. public static ContextAction("PHP-curl", "生成代码")
  483. function do30(arrSess: Session[]) { doStar(arrSess, "php","curl"); }
  484. public static ContextAction("PHP-http1", "生成代码")
  485. function do31(arrSess: Session[]) { doStar(arrSess, "php","http1"); }
  486. public static ContextAction("PHP-http2", "生成代码")
  487. function do32(arrSess: Session[]) { doStar(arrSess, "php","http2"); }
  488. public static function doStar(oSessions: Session[], target: String,client:String) {
  489. //注意看这里,请下载我给的这2个exe并替换成你电脑中正确的目录
  490. var httpsnippet = "E:\\fiddler生成代码\\httpsnippet.exe";
  491. var quicktype = "E:\\fiddler生成代码\\quicktype.exe";
  492. var oExportOptions = FiddlerObject.createDictionary();
  493. var tempPath2 = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler.har");
  494. if(System.IO.File.Exists(tempPath2)){
  495. System.IO.File.Delete(tempPath2);
  496. }
  497. var tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler.json");
  498. if(System.IO.File.Exists(tempPath)){
  499. System.IO.File.Delete(tempPath);
  500. }
  501. var tempRequestBodyPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_requestBody.json");
  502. if(System.IO.File.Exists(tempRequestBodyPath)){
  503. System.IO.File.Delete(tempRequestBodyPath);
  504. }
  505. var tempResponseBodyPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_responseBody.json");
  506. if(System.IO.File.Exists(tempResponseBodyPath)){
  507. System.IO.File.Delete(tempResponseBodyPath);
  508. }
  509. oExportOptions.Add("Filename", tempPath2);
  510. FiddlerApplication.DoExport("HTTPArchive v1.2", oSessions,oExportOptions, null);
  511. System.IO.File.Move(tempPath2, tempPath);
  512. if(!System.IO.File.Exists(tempPath)){
  513. MessageBox.Show("生成代码失败", "No action");
  514. return;
  515. }
  516. var rtPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_rt");
  517. if(System.IO.Directory.Exists(rtPath))System.IO.Directory.Delete(rtPath,true);
  518. if(!doProcess(httpsnippet, "\""+tempPath+"\" -t "+target+" -c "+client+" -o " + "\""+rtPath+"\"")){
  519. MessageBox.Show("生成代码错误", "No action");
  520. return;
  521. }
  522. var file = System.IO.Directory.GetFiles(rtPath);
  523. if(file.Length!=1){
  524. MessageBox.Show("生成代码错误", "No action");
  525. return;
  526. }
  527. var json = System.IO.File.ReadAllText(file[0]);
  528. System.IO.File.Delete(file[0]);
  529. var rtPath1 = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_request_body");
  530. if(System.IO.File.Exists(rtPath1))System.IO.File.Delete(rtPath1);
  531. if(!m_DisableReuqest && System.IO.File.Exists(tempRequestBodyPath)){
  532. json += getJsonCode(quicktype,tempRequestBodyPath,rtPath,rtPath1,target,"FiddlerRequest");
  533. }
  534. rtPath1 = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "fiddler_response_body");
  535. if(System.IO.File.Exists(rtPath1))System.IO.File.Delete(rtPath1);
  536. if(!m_DisableResponse && System.IO.File.Exists(tempResponseBodyPath)){
  537. json += getJsonCode(quicktype,tempResponseBodyPath,rtPath,rtPath1,target, "FiddlerReponse");
  538. }
  539. Clipboard.SetText(json);
  540. MessageBox.Show("代码生成成功,已复制到剪贴板");
  541. }
  542. static function getJsonCode(file: String,tempRequestBodyPath:String,rtPath:String,rtPath1:String,target:String,type:String): String {
  543. var json = "";
  544. var tmp1 = "";
  545. if(target == 'csharp'){
  546. tmp1 = "--quiet --telemetry disable --features just-types --array-type list --no-check-required --namespace \"Fiddlers\" --lang \"" + target + "\" --top-level \""+type+"Model\" \"" + tempRequestBodyPath + "\"" +" -o " + "\""+rtPath1+"\"";
  547. }
  548. else if(target == 'kotlin'){
  549. tmp1 = "--quiet --telemetry disable --framework just-types --lang \"" + target + "\" --top-level \""+type+"Model\" \"" + tempRequestBodyPath + "\"" +" -o " + "\""+rtPath1+"\"";
  550. }
  551. else if(target == 'java'){
  552. tmp1 = "--quiet --telemetry disable --array-type list --just-types --package \"Fiddlers\" --lang \"" + target + "\" --top-level \""+type+"Model\" \"" + tempRequestBodyPath + "\"" +" -o " + "\""+rtPath+"\\test"+"\"";
  553. }
  554. else {
  555. tmp1 = "--telemetry disable --just-types --lang \"" + target + "\" --top-level \""+type+"Models\" \"" + tempRequestBodyPath + "\"" +" -o " + "\""+rtPath1+"\"";
  556. }
  557. doProcess(file, tmp1)
  558. if(System.IO.File.Exists(rtPath1)){
  559. json += "\r\n//"+type+"-POJO\r\n" + System.IO.File.ReadAllText(rtPath1).Replace("package quicktype","");
  560. }
  561. if(target == 'java'){
  562. var javaFiles = System.IO.Directory.GetFiles(rtPath,"*.java");
  563. if(javaFiles.Length>0){
  564. json += "\r\n//"+type+"-POJO\r\n" ;
  565. for (var i:int = 0; i<javaFiles.Length; i++)
  566. {
  567. json += System.IO.File.ReadAllText(javaFiles[i]).Replace("package Fiddlers;","")
  568. System.IO.File.Delete(javaFiles[i]);
  569. }
  570. }
  571. }
  572. return json;
  573. }
  574. static function doProcess(file: String,paramsList:String): Boolean {
  575. var process = new System.Diagnostics.Process();
  576. process.StartInfo.FileName = file;
  577. process.StartInfo.Arguments = paramsList;
  578. process.StartInfo.CreateNoWindow = true;
  579. process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  580. process.StartInfo.UseShellExecute = false;
  581. process.StartInfo.Verb = "runas";
  582. process.StartInfo.RedirectStandardError = true;
  583. process.StartInfo.RedirectStandardOutput = true;
  584. process.Start();
  585. process.WaitForExit();
  586. process.Dispose();
  587. return true;
  588. }
  589. }

代码中两个exe的下载地址

链接:https://pan.baidu.com/s/1AaX8rGJjcQNd_tTZ35GwMQ 
提取码:hj0a

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/863369
推荐阅读
相关标签
  

闽ICP备14008679号