博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
换用代理IP的Webbrowser方法
阅读量:6498 次
发布时间:2019-06-24

本文共 5102 字,大约阅读时间需要 17 分钟。

用webbrowser做浏览器,换取代理IP是常用的功能,下面贴一段用到的换ip的代码!

View Code
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; }        }    }

使用函数

View Code
[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));        }

记在这里,以后学习!

转载于:https://www.cnblogs.com/pmars/archive/2012/06/28/2567599.html

你可能感兴趣的文章
在线html代码优化,网站seo优化html代码方法
查看>>
html中嵌入flv视频直播,html中嵌入flv格式文件的代码
查看>>
我的世界html启动器资源,我的世界hmcl启动器mod
查看>>
HTML如何把输入框变成必填值,required输入框为必填项
查看>>
在html中哪一个不是链接的目标属性,HTML试题
查看>>
android otg 挂载流程,android USB OTG功能如何打开及实现
查看>>
html属性board,pin_board.html
查看>>
html定位有几种,POSITION定位有哪几种?各有什么特点?
查看>>
苹果平板可以用html么,如何将ipad苹果平板电脑中的safari浏览器禁用
查看>>
背锅侠逆袭之路
查看>>
移动互联企业级应用三大场景
查看>>
vmware的APD和PDL详细解析
查看>>
理解:思科设备上的网络地址翻译功能(NAT)功能
查看>>
演示:使用协议分析器取证IPv6的报文结构
查看>>
oracle 11gr2 rac中的4种IP解说
查看>>
为什么你找不到工作?
查看>>
一名合格的测试员应具备的素质
查看>>
SCCM 2007系列3 配置
查看>>
Lync 小技巧-22-申请属于自己的域名
查看>>
20 个免费的 jQuery 的工具提示插件:
查看>>