以下是一个示例代码,演示如何使用WebBrowser控件来捕获重定向并下载动态生成的文件:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class WebBrowserHelper
{
    [DllImport("urlmon.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern Int32 URLDownloadToFile(IntPtr pCaller, string szURL, string szFileName, Int32 dwReserved, IntPtr lpfnCB);
    public static void DownloadFile(string url, string savePath)
    {
        using (WebBrowser browser = new WebBrowser())
        {
            browser.Navigate(url);
            browser.DocumentCompleted += (sender, e) =>
            {
                // 捕获重定向
                if (browser.Url.AbsoluteUri != url)
                {
                    string redirectUrl = browser.Url.AbsoluteUri;
                    // 使用URLDownloadToFile函数下载文件
                    URLDownloadToFile(IntPtr.Zero, redirectUrl, savePath, 0, IntPtr.Zero);
                }
                else
                {
                    // 动态生成的文件直接保存
                    browser.Document.ExecCommand("SaveAs", false, savePath);
                }
                // 关闭浏览器控件
                browser.Dispose();
            };
            Application.Run();
        }
    }
}
使用示例:
string url = "http://example.com/file"; // 替换为实际的URL
string savePath = "C:/path/to/save/file"; // 替换为实际的保存路径
WebBrowserHelper.DownloadFile(url, savePath);
注意:这是一个简单的示例,可能无法处理所有情况。在实际使用中,你可能需要根据具体的需求进行适当的修改和处理。