SQL代理作业运行.net c# WinForms应用程序与WebBrowser控件.试着发送按键

本文关键字:控件 WebBrowser 运行 作业 代理 net 应用程序 WinForms SQL | 更新日期: 2023-09-27 18:08:59

正如标题所示,我有一个带有WebBrowser控件的。net c# Winforms应用程序。我用它从一个网站下载一个(动态生成的)Excel文件,它工作得很好。我必须按照雇主的要求使用WebControl。

当我使用SQL Agent Job运行带有执行进程任务的SSIS包来执行应用程序时,它失败了(因为文件没有下载)。如果我运行SSIS包,它可以正常工作。不同之处在于SQL作业的应用程序窗口不显示。

当WebBrowser控件显示打开-下载-关闭对话框时,我发送按键(Alt + S)来保存文件,然后关闭对话框。当SQL Job运行应用程序时,击键永远不会到达webbrowser。

我已经尝试使用SendKeys发送密钥。SendWait,也使用User32 dll来获取对话框和按钮句柄,然后SendMessage/PostMessage(两者都尝试过)来发送击键。这两种方法作为exe和SSIS包都可以正常工作。在运行SQL作业时,这两种方法都失败。

我也试过重写下载管理器,但没有成功。说实话,我不知道该怎么做才能获得动态生成的Excel文件,当执行到达覆盖的下载方法时,应用程序必须下载。我在调试下载方法时得到的链接不是到文件的链接。

/// <summary>
/// Return S_OK (0) so that IE will stop to download the file itself. 
/// Else the default download user interface is used.
/// </summary>
public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo,
                    string pszHeaders, string pszRedir, uint uiCP)
{
    // Get the display name of the pointer to an IMoniker interface that specifies the object to be downloaded.
    string name;
    pmk.GetDisplayName(pbc, null, out name);
    if (!string.IsNullOrEmpty(name))
    {
        Uri url;
        if (Uri.TryCreate(name, UriKind.Absolute, out url))
        {
            if ( FileDownloading != null )
            {
                 FileDownloading(this, new FileDownloadEventArgs(url));
            }
            return Constants.S_OK;
        }
    }
    return 1;
}

这是当我点击按钮下载文件时的响应头:

Content-Disposition: attachment; filename="file.xlsx"
Content-Transfer-Encoding: binary
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

如果需要,我可以提供应用程序代码。

任何帮助都将非常感激。请原谅我的英语很差。

SQL代理作业运行.net c# WinForms应用程序与WebBrowser控件.试着发送按键

这听起来很傻。为什么你必须使用网页控件来下载并立即保存网页?因为SQL代理是作为服务运行的,你会给自己一个头痛的尝试与windows窗体应用程序。

我要么创建一个独立的非交互式控制台应用程序来进行下载和保存,或者如果重用性是动机-将应用程序分成两个:一个带有windows窗体的exe,另一个带有下载逻辑的exe。第一个exe可以引用另一个exe来访问它的公共类。