C# WebBrowser 设置独立的代理
近期需要对webbrower设置代理,找到了如下方案:
1.
对修改注册表,设置全局代理
这种方法限制在一个进程内只能设置或取消代理,而且是全局代理,对于部分不需要代理对功能会产生影响
2.
通过函数InternetSetOption实现修改当前程序中的WebBrowser设置
1 static class WebBrowserProxy 2 3 { 4 5 struct Struct_INTERNET_PROXY_INFO 6 7 { 8 9 public int dwAccessType; 10 11 public IntPtr proxy; 12 13 public IntPtr proxyBypass; 14 15 }; 16 17 18 19 [DllImport("wininet.dll", SetLastError = true)] 20 21 static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer,int lpdwBufferLength); 22 23 24 25 public static void SetProxy(string strProxy = "127.0.0.1:8888") 26 27 { 28 29 const int INTERNET_OPTION_PROXY = 38; 30 31 const int INTERNET_OPEN_TYPE_PROXY = 3; 32 33 34 35 Struct_INTERNET_PROXY_INFO struct_IPI; 36 37 38 39 // Filling in structure 40 41 struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY; 42 43 struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy); 44 45 struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local"); 46 47 48 49 // Allocating memory 50 51 IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI)); 52 53 54 55 // Converting structure to IntPtr 56 57 Marshal.StructureToPtr(struct_IPI, intptrStruct, true); 58 59 bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI)); 60 61 } 62 63 }
参考来源“https://blog.csdn.net/gold0523/article/details/38666701”
用自己的努力创造我们的未来