zoukankan      html  css  js  c++  java
  • 换用代理IP的Webbrowser方法

    用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));
            }

    记在这里,以后学习!

  • 相关阅读:
    2019年春季学期第三周作业
    2019春第二周作业+一些的挑战作业
    查找整数
    寒假作业3编辑总结
    寒假作业2编辑总结
    对自己影响最大的老师
    2019春第九周作业
    2019春第八周作业
    2019春第七周作业
    2019春第六周作业
  • 原文地址:https://www.cnblogs.com/pmars/p/2567599.html
Copyright © 2011-2022 走看看