C # WebBrowrse interception download dialog box,

Source: Internet
Author: User

C # WebBrowrse interception download dialog box,

In order to implement this function, it may be a lot of time, and the results of the online search are basically how to shield the warning dialog box. Later, I asked a technical expert (Starts_2000, the main author of the programmer's window). He implemented it using C ++. He tried C # And did not solve it. Then he was busy with other things, I will use the C ++ dynamic library later. At that time, my testing environment was win7 64-bit + vs2013, and later the system environment changed to win10 + vs2015, suddenly it was not easy to make it. I quickly re-compiled the C ++ source code and made various errors, so I felt so unstable. What should I do? I can't ask anyone for help anymore. When the next environment changes, I will continue to ask others? This requires you to use C # for implementation. Now let's enter the topic.

WebBrowser requests an address, such as a http://download.easyicon.net/png/568613/48/ that appears:

Purpose: you can add your own code before the above dialog box is displayed for control. For example

 

To enable this function, I found some help information on the Internet:

Http://stackoverflow.com/questions/13362922/windows-forms-webbrowswer-control-with-idownloadmanager

 

There are two links to the same problem:

Https://q.cnblogs.com/q/57083/

Http://stackoverflow.com/questions/32513190/dllimportregistercallbackpbc-url-and-bindmonikertostreampmk-pbc

 

After reading these documents, I encountered the same problems as them. The main problems are as follows:

1. The complete Sample Code cannot be found. You can only look at it.

2. the breakpoint in the Download method is not included, and of course there is no Interception Effect.

3. We hope that not all downloads need to be intercepted. Some of them use the built-in functions of IE for download.

The first problem: search for some code to sort it out.

Second problem: I found that the interface definition was incorrect at the time.

   [ComImport, Guid("6d5140c1-7436-11ce-8034-00aa006009fa"), 
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown), ComVisible(false)]    public interface IServiceProvider    {        [return: MarshalAs(UnmanagedType.I4)]        [PreserveSig]        int QueryService(ref Guid guidService,   ref Guid riid,                                   /*[MarshalAs(UnmanagedType.Interface)]*/   IntPtr ppvObject);    }

You can delete the comments. If there is still a breakpoint, refer to the complete source code.

Third, the system API is required, and many people do not know how to define this API. For example, a foreigner asked me about the above link. The API is defined as follows:

internal class API    {        [DllImport("ole32.dll")]        internal static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc);        [DllImport("ole32.dll")]        internal static extern int RegisterCallback(IBindCtx pbc, Uri url);        [DllImport("ole32.dll")]        internal static extern int BindMonikerToStream(IMoniker pmk, IBindCtx ppbc);    }
The test shows that you only need to call the second or third API to implement built-in IE download.
 
Some code is as follows:
Private void DownLoadDemo () {try {DownLoadWebBrowser = new DownLoadWebBrowser (); DownLoadWebBrowser. fileDownloading + = DownLoadWebBrowser_FileDownloading; DownLoadWebBrowser. navigate (downLoadUrl);} catch (Exception) {throw;} private void DownLoadWebBrowser_FileDownloading (object sender, FileDownloadEventArgs e) {string info = string. format ("{0}, whether to continue", e. url. toString (); bool downLoad = MessageBox. show (info, "select", MessageBoxButtons. yesNo, MessageBoxIcon. question) = DialogResult. no; e. cancel = downLoad ;}

Download the complete code.

In addition to blocking downloading, the source code also comes with some other functions (blocking the dialog box, JS and C # code calls, and IE versions), which were previously written, just get rid of it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.