浏览器自动化的一些体会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,这个可以和上面的一起用,从而既支持代理切换,也支持设置某些浏览器选项。
分类:
浏览器自动化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律