fiddlerCore


Fiddlerのカスタマイズ用API

fiddlerCoreサンプル
http://fiddler.wikidot.com/fiddlercore-demo

Nuget

Install-Package FiddlerCore

自プロセスをFiddlerプロキシ経由にする。
デバッグ以外の用途があまり思い浮かばないのでエラーの再現とかかな。
レスポンス差し替えとかリクエスト差し替えも出来なくはなさそうだが使い道がないか。

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JsonDebug
{
  public partial class Form1 : Form
  {
    private void onClick(object sender, EventArgs e)
    {
      webBrowser1.Navigate(textBox1.Text);
    }

    public Form1()
    {
      InitializeComponent();

      this.button1.Click += this.onClick;

      Fiddler.FiddlerApplication.AfterSessionComplete
                  += this.OnAfterSessionComplete;

      Fiddler.FiddlerApplication.Startup(0, Fiddler.FiddlerCoreStartupFlags.ChainToUpstreamGateway);

      Fiddler.URLMonInterop.SetProxyInProcess(string.Format("127.0.0.1:{0}",
                  Fiddler.FiddlerApplication.oProxy.ListenPort), "<local>");
    }

    void OnAfterSessionComplete(Fiddler.Session oSession)
    {
      Debug.Print("{0}:{1}", oSession.fullUrl, oSession.oResponse.MIMEType);

      var res = oSession.oResponse;
      if (res.MIMEType == "text/plain")
      {
        var json = oSession.GetResponseBodyAsString();

        json = this.FormatJson(json);
        //dynamic jsonObj = JsonConvert.DeserializeObject(json);
        //json = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);

        Debug.Print(json);
      }//end if
    }

    private const string INDENT_STRING = "  ";

    private string FormatJson(string json)
    {

      int indentation = 0;
      int quoteCount = 0;
      var result =
          from ch in json
          let quotes = ch == '"' ? quoteCount++ : quoteCount
          let lineBreak = ch == ',' && quotes % 2 == 0 ? ch + Environment.NewLine + String.Concat(Enumerable.Repeat(INDENT_STRING, indentation)) : null
          let openChar = ch == '{' || ch == '[' ? ch + Environment.NewLine + String.Concat(Enumerable.Repeat(INDENT_STRING, ++indentation)) : ch.ToString()
          let closeChar = ch == '}' || ch == ']' ? Environment.NewLine + String.Concat(Enumerable.Repeat(INDENT_STRING, --indentation)) + ch : ch.ToString()
          select lineBreak == null
                      ? openChar.Length > 1
                          ? openChar
                          : closeChar
                      : lineBreak;

      return String.Concat(result);
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
      Fiddler.URLMonInterop.ResetProxyInProcessToDefault();

      Fiddler.FiddlerApplication.Shutdown();
    }

  }
}
カテゴリー: 開発 タグ: パーマリンク