官方版

Build a Custom Importer or Exporter

Sample Extension

  1. Create a Fiddler extension project.
  2. Modify the default class1.cs (or create a new class) in your project as follows: ```vbnet using System; using System.IO; using System.Text; using System.Windows.Forms; using Fiddler; using System.Diagnostics; using System.Reflection; [assembly: AssemblyVersion(“1.0.0.0”)] [assembly: Fiddler.RequiredVersion(“2.4.0.0”)]

[ProfferFormat(“TAB-Separated Values”, “Session List in Tab-Delimited Format”)] [ProfferFormat(“Comma-Separated Values”, “Session List in Comma-Delimited Format; import into Excel or other tools”)]

public class CSVTranscoder: ISessionExporter // Ensure class is public, or Fiddler won’t see it! { public bool ExportSessions(string sFormat, Session[] oSessions, Dictionary dictOptions, EventHandler evtProgressNotifications) { bool bResult = false; string chSplit;

  1. // Determine if we already have a filename from the dictOptions collection
  2. string sFilename = null;
  3. if (null != dictOptions && dictOptions.ContainsKey("Filename"))
  4. {
  5. sFilename = dictOptions["Filename"] as string;
  6. }
  7. if (sFormat == "Comma-Separated Values")
  8. {
  9. chSplit = ",";
  10. if (string.IsNullOrEmpty(sFilename)) sFilename = Fiddler.Utilities.ObtainSaveFilename("Export As " + sFormat, "CSV Files (*.csv)|*.csv");
  11. }
  12. else
  13. {
  14. chSplit = "\t";
  15. if (string.IsNullOrEmpty(sFilename)) sFilename = Fiddler.Utilities.ObtainSaveFilename("Export As " + sFormat, "TSV Files (*.tsv)|*.tsv");
  16. }
  17. if (String.IsNullOrEmpty(sFilename)) return false;
  18. try
  19. {
  20. StreamWriter swOutput = new StreamWriter(sFilename, false, Encoding.UTF8);
  21. int iCount = 0;
  22. int iMax = oSessions.Length;
  23. #region WriteColHeaders
  24. bool bFirstCol = true;
  25. foreach (ColumnHeader oLVCol in FiddlerApplication.UI.lvSessions.Columns)
  26. {
  27. if (!bFirstCol)
  28. {
  29. swOutput.Write(chSplit);
  30. }
  31. else
  32. {
  33. bFirstCol = false;
  34. }
  35. swOutput.Write(oLVCol.Text.Replace(chSplit, ""));
  36. }
  37. swOutput.WriteLine();
  38. #endregion WriteColHeaders
  39. #region WriteEachSession
  40. foreach (Session oS in oSessions)
  41. {
  42. iCount++;
  43. if (null != oS.ViewItem)
  44. {
  45. bFirstCol = true;
  46. ListViewItem oLVI = (oS.ViewItem as ListViewItem);
  47. if (null == oLVI) continue;
  48. foreach (ListViewItem.ListViewSubItem oLVC in oLVI.SubItems)
  49. {
  50. if (!bFirstCol)
  51. {
  52. swOutput.Write(chSplit);
  53. }
  54. else
  55. {
  56. bFirstCol = false;
  57. }
  58. swOutput.Write(oLVC.Text.Replace(chSplit, ""));
  59. }
  60. swOutput.WriteLine();
  61. }
  62. if (null != evtProgressNotifications)
  63. {
  64. evtProgressNotifications(null, new ProgressCallbackEventArgs(, ));
  65. ProgressCallbackEventArgs PCEA = new ProgressCallbackEventArgs((iCount/(float)iMax), "wrote " + iCount.ToString() + " records.");
  66. evtProgressNotifications(null, PCEA);
  67. if (PCEA.Cancel) { swOutput.Close(); return false; }
  68. }
  69. }
  70. #endregion WriteEachSession
  71. swOutput.Close();
  72. bResult = true;
  73. }
  74. catch (Exception eX)
  75. {
  76. MessageBox.Show(eX.Message, "Failed to export");
  77. bResult = false;
  78. }

} return bResult; }

public void Dispose() { } }

  1. 1. [Compile and load your extension in Fiddler](https://docs.telerik.com/fiddler/Extend-Fiddler/LoadExtension).<br />
  2. <a name="see-also"></a>
  3. ## [See Also](https://docs.telerik.com/fiddler/Extend-Fiddler/BuildImporterExporter#see-also)
  4. [Build extension assemblies to run in both Fiddler 2 and 4](https://docs.telerik.com/fiddler/Extend-Fiddler/ExtensionsForv2Andv4)
  5. <a name="t44ST"></a>
  6. # 谷歌翻译版
  7. <a name="8vbkx"></a>
  8. # [建立自定义导入或者导出](https://docs.telerik.com/fiddler/Extend-Fiddler/BuildImporterExporter#build-a-custom-importer-or-exporter)
  9. <a name="md8WD"></a>
  10. ## [示列扩展](https://docs.telerik.com/fiddler/Extend-Fiddler/BuildImporterExporter#sample-extension)
  11. 1. 创建一个 [Fiddler 扩展项目](https://docs.telerik.com/fiddler/Extend-Fiddler/CreateExtension).
  12. 1. 在项目中修改默认的class1.cs(或创建一个新类),如下所示:
  13. ```vbnet
  14. using System;
  15. using System.IO;
  16. using System.Text;
  17. using System.Windows.Forms;
  18. using Fiddler;
  19. using System.Diagnostics;
  20. using System.Reflection;
  21. [assembly: AssemblyVersion("1.0.0.0")]
  22. [assembly: Fiddler.RequiredVersion("2.4.0.0")]
  23. [ProfferFormat("TAB-Separated Values", "Session List in Tab-Delimited Format")]
  24. [ProfferFormat("Comma-Separated Values",
  25. "Session List in Comma-Delimited Format; import into Excel or other tools")]
  26. public class CSVTranscoder: ISessionExporter // Ensure class is public, or Fiddler won't see it!
  27. {
  28. public bool ExportSessions(string sFormat, Session[] oSessions, Dictionary<string, object> dictOptions,
  29. EventHandler<ProgressCallbackEventArgs> evtProgressNotifications)
  30. {
  31. bool bResult = false;
  32. string chSplit;
  33. // Determine if we already have a filename from the dictOptions collection
  34. string sFilename = null;
  35. if (null != dictOptions && dictOptions.ContainsKey("Filename"))
  36. {
  37. sFilename = dictOptions["Filename"] as string;
  38. }
  39. if (sFormat == "Comma-Separated Values")
  40. {
  41. chSplit = ",";
  42. if (string.IsNullOrEmpty(sFilename)) sFilename = Fiddler.Utilities.ObtainSaveFilename("Export As " + sFormat, "CSV Files (*.csv)|*.csv");
  43. }
  44. else
  45. {
  46. chSplit = "\t";
  47. if (string.IsNullOrEmpty(sFilename)) sFilename = Fiddler.Utilities.ObtainSaveFilename("Export As " + sFormat, "TSV Files (*.tsv)|*.tsv");
  48. }
  49. if (String.IsNullOrEmpty(sFilename)) return false;
  50. try
  51. {
  52. StreamWriter swOutput = new StreamWriter(sFilename, false, Encoding.UTF8);
  53. int iCount = 0;
  54. int iMax = oSessions.Length;
  55. #region WriteColHeaders
  56. bool bFirstCol = true;
  57. foreach (ColumnHeader oLVCol in FiddlerApplication.UI.lvSessions.Columns)
  58. {
  59. if (!bFirstCol)
  60. {
  61. swOutput.Write(chSplit);
  62. }
  63. else
  64. {
  65. bFirstCol = false;
  66. }
  67. swOutput.Write(oLVCol.Text.Replace(chSplit, ""));
  68. }
  69. swOutput.WriteLine();
  70. #endregion WriteColHeaders
  71. #region WriteEachSession
  72. foreach (Session oS in oSessions)
  73. {
  74. iCount++;
  75. if (null != oS.ViewItem)
  76. {
  77. bFirstCol = true;
  78. ListViewItem oLVI = (oS.ViewItem as ListViewItem);
  79. if (null == oLVI) continue;
  80. foreach (ListViewItem.ListViewSubItem oLVC in oLVI.SubItems)
  81. {
  82. if (!bFirstCol)
  83. {
  84. swOutput.Write(chSplit);
  85. }
  86. else
  87. {
  88. bFirstCol = false;
  89. }
  90. swOutput.Write(oLVC.Text.Replace(chSplit, ""));
  91. }
  92. swOutput.WriteLine();
  93. }
  94. if (null != evtProgressNotifications)
  95. {
  96. evtProgressNotifications(null, new ProgressCallbackEventArgs(, ));
  97. ProgressCallbackEventArgs PCEA = new ProgressCallbackEventArgs((iCount/(float)iMax), "wrote " + iCount.ToString() + " records.");
  98. evtProgressNotifications(null, PCEA);
  99. if (PCEA.Cancel) { swOutput.Close(); return false; }
  100. }
  101. }
  102. #endregion WriteEachSession
  103. swOutput.Close();
  104. bResult = true;
  105. }
  106. catch (Exception eX)
  107. {
  108. MessageBox.Show(eX.Message, "Failed to export");
  109. bResult = false;
  110. }
  111. }
  112. return bResult;
  113. }
  114. public void Dispose()
  115. {
  116. }
  117. }
  1. 在Fiddler编译并且加载扩展.

    参见

    构建可在Fiddler2 和 4中运行的扩展程序集