正如標題所示,我有一個帶有WebBrowser控件的.NET C#Winforms應用程序。我用它從網站上下載(動態生成的)Excel文件,它工作得很好。根據我的僱主要求,我必須使用WebControl。使用WebBrowser控件運行.NET C#WinForms應用程序的SQL Agent作業。嘗試發送按鍵
當我使用SQL代理作業運行帶有執行進程任務的SSIS包來執行應用程序時,它失敗(因爲文件未下載)。如果我運行SSIS包,它可以正常工作。不同之處在於應用程序窗口沒有顯示的SQL Job。
當WebBrowser控件顯示打開 - 下載 - 關閉對話框時,我發送按鍵筆劃(Alt + S)來保存文件,然後關閉對話框。當SQL作業運行應用程序時,擊鍵從不會將其傳送到WebBroser。
我已經嘗試使用SendKeys.SendWait發送密鑰,並且還使用User32 dll獲取對話框和按鈕句柄,然後使用SendMessage/PostMessage(嘗試兩者)來發送擊鍵。這兩種方法都可以像exe一樣正常工作,也可以作爲SSIS包使用。運行SQL作業時,這兩種方法都會失敗。
我也嘗試重寫下載管理器,建議here沒有成功。說實話,我不知道該怎麼做,以獲得動態生成的Excel文件時,應用程序必須下載,當執行到達被覆蓋的下載方法。調試Download方法時得到的鏈接不是指向該文件的鏈接。
/// <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
我可以,如果要求在應用程序代碼提供。
任何幫助將非常感激。請原諒我可憐的英語。
Regards
這很愚蠢。不幸的是,我不允許在開發機器中使用Fiddler,並且我被要求使用WebBrowser類。我試圖實現這個解決方案,但我有點卡住:http://stackoverflow.com/questions/13362922/windows-forms-webbrowswer-control-with-idownloadmanager – 1Tester