0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fiddler ファイル出力

Last updated at Posted at 2025-03-12

ステップ 1:Fiddler Script を開く
Fiddler を開き、メニューの「Rules」 → 「Customize Rules」をクリックする。
「CustomRules.js」 ファイルを開き、以下のコードを見つける。

static function OnBeforeRequest(oSession: Session) {
ステップ 2:Excel ファイルのダウンロードルールを追加する
OnBeforeRequest(oSession: Session) メソッドの 末尾 に以下のコードを追加する。


//if (oSession.uriContains("/api/download")) {  // API URL が一致するか確認
//    oSession.utilCreateResponseAndBypassServer();  // Fiddler で直接レスポンスを返し、サーバーにはリクエストを送らない
if (oSession.fullUrl.Contains("https://example.com/api/download")) {  // API URL が一致するか確認    oSession.utilCreateResponseAndBypassServer(); 

    try {
        // 
        var filePath = "C:\\temp\\test.xlsx";

        if (!System.IO.File.Exists(filePath)) {
            oSession.utilSetResponseBody("File not Founded:" + filePath);
            return;
        }

        var fileBytes = System.IO.File.ReadAllBytes(filePath);
        var fileSize = fileBytes.Length;

        oSession.oResponse.headers.SetStatus(200, "OK");
        oSession.oResponse["Content-Type"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        oSession.oResponse["Content-Disposition"] = "attachment; filename*=UTF-8''test.xlsx";
        oSession.oResponse["Content-Length"] = fileSize.ToString();

        oSession.responseBodyBytes = fileBytes;
    } catch (Exception ex) {
        oSession.utilSetResponseBody("Read Filed: " + ex.Message);
    }
}

ステップ 3:保存して Fiddler を再起動する
「Ctrl + S」 を押して CustomRules.js を保存する。
Fiddler を閉じて再起動し、ルールを適用する。
ステップ 4:テスト
ブラウザで https://example.com/api/download にアクセスする。
「测试文件.xlsx」 が正常にダウンロードされ、ファイル名が文字化けしないことを確認する。

補足;
(echo HTTP/1.1 200 OK
echo Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
echo Content-Disposition: attachment; filename=test.xlsx
echo.
type C:\temp\test.xlsx) > C:\temp\response.dat
補足2;

using System;
using System.IO;
using System.Net;
using System.Text;

class Program
{
    static void Main()
    {
        // 現在のプログラムの実行ディレクトリを取得
        string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;

        // ユーザーにExcelファイル名を入力させる(拡張子も含める)
        Console.WriteLine("Excelファイル名を入力してください(例:テストファイル.xlsx)");
        string fileName = Console.ReadLine();

        // フルパスを作成(プログラムと同じディレクトリ内)
        string filePath = Path.Combine(currentDirectory, fileName);

        // ファイルが存在するか確認
        if (!File.Exists(filePath))
        {
            Console.WriteLine($"❌ エラー:ファイル '{fileName}' が見つかりません!");
            return;
        }

        // ファイル名をUTF-8でエンコード(URLエンコード)
        string encodedFileName = WebUtility.UrlEncode(fileName);
        string contentDisposition = $"attachment; filename*=UTF-8''{encodedFileName}";

        // HTTPレスポンス用の `response.dat` のパス
        string responseFile = Path.Combine(currentDirectory, "response.dat");

        try
        {
            using (FileStream fs = new FileStream(responseFile, FileMode.Create, FileAccess.Write))
            using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8))
            {
                // HTTPヘッダーを書き込む
                writer.WriteLine("HTTP/1.1 200 OK");
                writer.WriteLine("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                writer.WriteLine($"Content-Disposition: {contentDisposition}");
                writer.WriteLine(); // 空行(ヘッダーとボディを区切る)
                writer.Flush(); // ヘッダーを確実にディスクへ書き込む

                // Excelファイルをバイト配列として読み込み、`response.dat` に書き込む
                byte[] fileBytes = File.ReadAllBytes(filePath);
                fs.Write(fileBytes, 0, fileBytes.Length);
            }

            Console.WriteLine($" HTTPレスポンスファイルを作成しました: {responseFile}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($" エラー発生:{ex.Message}");
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?