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

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

转自:

用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/wifi/articles/2777003.html

你可能感兴趣的文章
选择器 :gt(index)
查看>>
notes on python
查看>>
kafa
查看>>
资源 | Feature Tools:可自动构造机器学习特征的Python库
查看>>
linux Shell 中常用的条件判断
查看>>
angular 动态设置blob链接给 ng-href时遇到unsafe 解决方案
查看>>
Java与Highcharts实例(四) - Hello Highcharts (后台Java传递数
查看>>
连接数据库的操作 总结
查看>>
Android 小米手机开发APP图标更换后还显示原来的图标
查看>>
在代码中修改Shape的solid属性的color值
查看>>
MySQL字符集问题
查看>>
Java多线程总结
查看>>
iPad Mini外屏碎了 换屏幕教程
查看>>
LinkedBlockingQueue操作,线程安全问题,ConcurrentModificationException 异常分析与解决方案...
查看>>
redis3.2新功能--GEO地理位置命令介绍与实战开发
查看>>
java 通过ssh 执行命令
查看>>
算法导论——基数排序(基于计数排序)
查看>>
19.TCP的交互数据流
查看>>
字符串匹配的Boyer-Moore算法
查看>>
memcached数据库未授权访问漏洞解决
查看>>