浏览器自动化的一些体会6 增强的webBrowser控件

这里谈两点

1.支持代理服务器切换

一种方法是修改注册表,不是太好的做法,而且,只能改全局设置,不能改局部(比如只让当前的webBrowser控件使用代理,而其他应用不用代理)

另外一个较好的方法,示例代码可以从这里下载:https://code.msdn.microsoft.com/windowsapps/CSWebBrowserWithProxy-c8535715

 这个代码来自msdn,自然很权威,也确实很不错,即使不熟悉pinvoke的操作也可拿来用。美中不足的是这个代码缺少禁用代理服务器的功能,我模仿WinINet.cs里的SetConnectionProxyInternal方法,自己写了一个方法:

        public static bool DisableSystemProxy()
        {
            IntPtr hInternet = IntPtr.Zero;
            INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[1];

            // Set PROXY flags.
            Options[0] = new INTERNET_PER_CONN_OPTION();
            Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
            Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_DIRECT;

            // Allocate a block of memory of the options.
            System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0]));

            System.IntPtr current = buffer;

            // Marshal data from a managed object to an unmanaged block of memory.
            Marshal.StructureToPtr(Options[0], current, false);

            // Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
            INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST();

            // Point to the allocated memory.
            option_list.pOptions = buffer;

            // Return the unmanaged size of an object in bytes.
            option_list.Size = Marshal.SizeOf(option_list);

            // IntPtr.Zero means LAN connection.
            option_list.Connection = IntPtr.Zero;

            option_list.OptionCount = Options.Length;
            option_list.OptionError = 0;
            int size = Marshal.SizeOf(option_list);

            // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance.
            IntPtr intptrStruct = Marshal.AllocCoTaskMem(size);

            // Marshal data from a managed object to an unmanaged block of memory.
            Marshal.StructureToPtr(option_list, intptrStruct, true);

            // Set internet settings.
            bool bReturn = NativeMethods.InternetSetOption(
                hInternet,
                INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
                intptrStruct, size);

            // Free the allocated memory.
            Marshal.FreeCoTaskMem(buffer);
            Marshal.FreeCoTaskMem(intptrStruct);

            // Throw an exception if this operation failed.
            if (!bReturn)
            {
                throw new ApplicationException(" Set Internet Option Failed!");
            }

            // Notify the system that the registry settings have been changed and cause
            // the proxy data to be reread from the registry for a handle.
            NativeMethods.InternetSetOption(
                hInternet,
                INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED,
                IntPtr.Zero, 0);

            NativeMethods.InternetSetOption(
                hInternet,
                INTERNET_OPTION.INTERNET_OPTION_REFRESH,
                IntPtr.Zero, 0);

            return bReturn;
        }

2.设置浏览器选项,如不加载图片

可参考这个链接:https://stackoverflow.com/questions/7608550/implement-idispatchinvoke-to-be-called-by-a-webbrowser-control,这个可以和上面的一起用,从而既支持代理切换,也支持设置某些浏览器选项。

posted @ 2018-01-10 02:18  平静寄居者  阅读(294)  评论(0编辑  收藏  举报