Элемент управления Windows Forms Webbrowswer с помощью IDownloadManager
Я используюSystems.Windows.Forms.Webbrowser
контроль и необходимость переопределить менеджер загрузок. Я следовал инструкциямВот создать подкласс формы и переопределитьCreateWebBrowserSiteBase()
/// <summary>
/// Browser with download manager
/// </summary>
public class MyBrowser : WebBrowser
{
/// <summary>
/// Returns a reference to the unmanaged WebBrowser ActiveX control site,
/// which you can extend to customize the managed <see ref="T:System.Windows.Forms.WebBrowser"/> control.
/// </summary>
/// <returns>
/// A <see cref="T:System.Windows.Forms.WebBrowser.WebBrowserSite"/> that represents the WebBrowser ActiveX control site.
/// </returns>
protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
{
var manager = new DownloadWebBrowserSite(this);
manager.FileDownloading += (sender, args) =>
{
if (FileDownloading != null)
{
FileDownloading(this, args);
}
};
return manager;
}
}
вDownloadWebBrowserSite
Я реализуюIServiceProvider
обеспечитьIDownloadManager
по запросу.
/// <summary>
/// Queries for a service
/// </summary>
/// <param name="guidService">the service GUID</param>
/// <param name="riid"></param>
/// <param name="ppvObject"></param>
/// <returns></returns>
public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
{
if ( (guidService == Constants.IID_IDownloadManager && riid == Constants.IID_IDownloadManager ))
{
ppvObject = Marshal.GetComInterfaceForObject(_manager, typeof(IDownloadManager));
return Constants.S_OK;
}
ppvObject = IntPtr.Zero;
return Constants.E_NOINTERFACE;
}
DownloadManager
взято из примера выше.
/// <summary>
/// Intercepts downloads of files, to add as PDFs or suppliments
/// </summary>
[ComVisible(true)]
[Guid("bdb9c34c-d0ca-448e-b497-8de62e709744")]
[CLSCompliant(false)]
public class DownloadManager : IDownloadManager
{
/// <summary>
/// event called when the browser is about to download a file
/// </summary>
public event EventHandler<FileDownloadEventArgs> FileDownloading;
/// <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;
}
}
Проблема в том, чтоpmk.GetDisplayName
возвращает начальный URL-адрес, а не URL-адрес элемента для загрузки. Если URI указывает на динамическую страницу, такую какhttp://www.example.com/download.phpЯ не получаю фактический файл для загрузки. Мне нужно получить URL из заголовка, чтобы получить фактический файл, который я должен загружать.
Google указывает, что я должен создатьIBindStatusCallback
реализация, которая также реализуетIHttpNegotiate
а такжеIServiceProvider
в ответ наIID_IHttpNegotiate
так, чтобы я мог видетьIHttpNegotiate.OnResponse
, Мне удалось реализовать это, однако,QueryService
только кажется, что когда-либо проситьIID_IInternetProtocol
и никогдаIID_IHttpNegotiate
.
Будем признательны любому совету.