開発環境 Microsoft Visual Studio Express 2013 for Windows Desktop
実行環境 Microsoft Windows 8.1 (64bit)
プロジェクトの種類 Visual C#/コンソール アプリケーション
プロジェクト名 kcsapiTest

Program.cs
// プロジェクト参照:FiddlerCore4.dll
 
using Fiddler;
using System;
using System.IO;
using System.Threading;
 
namespace kcsapiTest
{
    class Program
    {
        private static void WriteCommandResponse(string s)
        {
            ConsoleColor oldColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(s);
            Console.ForegroundColor = oldColor;
        }
 
        private static void DoQuit()
        {
            WriteCommandResponse("Shutting down...");
            FiddlerApplication.Shutdown();
            Thread.Sleep(500);
        }
 
        static void Main(string[] args)
        {
            FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
 
            Console.CancelKeyPress += Console_CancelKeyPress;
 
            Console.WriteLine("Starting {0}...", FiddlerApplication.GetVersionString());
 
            FiddlerApplication.Startup(0, true, true);
            Console.WriteLine("Hit CTRL+C to end session.");
 
            bool bDone = false;
            do
            {
                ConsoleKeyInfo cki = Console.ReadKey();
                Console.WriteLine();
                switch (cki.KeyChar)
                {
                    case 'q':
                        bDone = true;
                        DoQuit();
                        break;
                }
            } while (!bDone);
        }
 
        static void FiddlerApplication_AfterSessionComplete(Session oSession)
        {
            Console.WriteLine("Finished session:[{1}] [{0}]", oSession.fullUrl, oSession.oResponse.MIMEType);
 
            string[] url = oSession.fullUrl.Split('/');
            if (5 <= url.Length && url[3] == "kcsapi")
            {
                if (oSession.oResponse.MIMEType == "text/plain")
                {
                    string req = oSession.GetRequestBodyAsString();
                    string res = oSession.GetResponseBodyAsString();
 
                    string dir = string.Join("/", url, 4, url.Length - 4);
                    string fname = "";
                    switch (dir)
                    {
                        case "api_start2":
                            fname = "api_start2";
                            break;
                        case "api_port/port":
                            fname = "api_port";
                            break;
                        case "api_get_member/slot_item":
                            fname = "slot_item";
                            break;
                    }
                    if (fname != "")
                    {
                        using (StreamWriter sw = new StreamWriter(fname + ".js"))
                        {
                            sw.Write(res);
                        }
                    }
 
                    using (StreamWriter sw = new StreamWriter("Response.log", true))   // append
                    {
                        sw.WriteLine(oSession.fullUrl + "\t" + req + "\t" + res);
                    }
                }
            }
        }
 
        static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            DoQuit();
        }
    }
}
 
最終更新:2014年05月23日 04:22