换用代理IP的Webbrowser方法
用webbrowser做浏览器,换取代理IP是常用的功能,下面贴一段用到的换ip的代码!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class MyWebBrowser : WebBrowser { [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch), TypeLibType(TypeLibTypeFlags.FHidden)] public interface DWebBrowserEvents2 { [DispId(271)] void NavigateError( [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp, [In] ref object URL, [In] ref object frame, [In] ref object statusCode, [In, Out] ref bool cancel); } AxHost.ConnectionPointCookie cookie; MyWebBrowserEventHelper helper; public delegate void WebBrowserNavigateErrorEventHandler(object sender, WebBrowserNavigateErrorEventArgs e); [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")] protected override void CreateSink() { base.CreateSink(); // Create an instance of the client that will handle the event // and associate it with the underlying ActiveX control. helper = new MyWebBrowserEventHelper(this); cookie = new AxHost.ConnectionPointCookie( this.ActiveXInstance, helper, typeof(DWebBrowserEvents2)); } [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")] protected override void DetachSink() { // Disconnect the client that handles the event // from the underlying ActiveX control. if (cookie != null) { cookie.Disconnect(); cookie = null; } base.DetachSink(); } public event WebBrowserNavigateErrorEventHandler NavigateError; // Raises the NavigateError event. protected virtual void OnNavigateError( WebBrowserNavigateErrorEventArgs e) { if (this.NavigateError != null) { this.NavigateError(this, e); } } // Handles the NavigateError event from the underlying ActiveX // control by raising the NavigateError event defined in this class. private class MyWebBrowserEventHelper : StandardOleMarshalObject, DWebBrowserEvents2 { private MyWebBrowser parent; public MyWebBrowserEventHelper(MyWebBrowser parent) { this.parent = parent; } public void NavigateError(object pDisp, ref object url, ref object frame, ref object statusCode, ref bool cancel) { // Raise the NavigateError event. this.parent.OnNavigateError( new WebBrowserNavigateErrorEventArgs( (String)url, (String)frame, (Int32)statusCode, cancel)); } } } public class WebBrowserNavigateErrorEventArgs : EventArgs { private String urlValue; private String frameValue; private Int32 statusCodeValue; private Boolean cancelValue; public WebBrowserNavigateErrorEventArgs( String url, String frame, Int32 statusCode, Boolean cancel) { urlValue = url; frameValue = frame; statusCodeValue = statusCode; cancelValue = cancel; } public String Url { get { return urlValue; } set { urlValue = value; } } public String Frame { get { return frameValue; } set { frameValue = value; } } public Int32 StatusCode { get { return statusCodeValue; } set { statusCodeValue = value; } } public Boolean Cancel { get { return cancelValue; } set { cancelValue = value; } } }
使用函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[DllImport("wininet.dll", SetLastError = true)] private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength); public struct Struct_INTERNET_PROXY_INFO { public int dwAccessType; public IntPtr proxy; public IntPtr proxyBypass; }; private void RefreshIESettings(string strProxy) { const int INTERNET_OPTION_PROXY = 38; const int INTERNET_OPEN_TYPE_PROXY = 3; Struct_INTERNET_PROXY_INFO struct_IPI; // Filling in structure struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY; struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy); struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local"); // Allocating memory IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI)); // Converting structure to IntPtr Marshal.StructureToPtr(struct_IPI, intptrStruct, true); bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI)); }
记在这里,以后学习!