zoukankan      html  css  js  c++  java
  • C# WebBrowser设置代理

    WebBrowser控件是基于IE浏览器的,所以它的内核功能是依赖于IE.

    code:
     
        class IEProxy
        {
            //设置代理选项                
            private const int INTERNET_OPTION_PROXY = 38;
            //设置代理类型                
            private const int INTERNET_OPEN_TYPE_PROXY = 3;                
            //设置代理类型,直接访问,不需要通过代理服务器
            private const int INTERNET_OPEN_TYPE_DIRECT = 1;
    
            private string ProxyStr;
    
            //You can change the proxy with InternetSetOption method from the wininet.dll                
            //这个就是设置一个Internet 选项。设置代理是Internet 选项其中的一个功能
            [System.Runtime.InteropServices.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;
            }
           //设置代理的方法
           //strProxy为代理IP:端口        
            private bool InternetSetOption(string strProxy)
            {
                int bufferLength;
                IntPtr intptrStruct;
                Struct_INTERNET_PROXY_INFO struct_IPI;
                
                //Filling in structure 
                if (string.IsNullOrEmpty(strProxy) || strProxy.Trim().Length == 0)
                {
                    strProxy = string.Empty;
                    struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
    
                }
                else
                {
                    struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
                }
                //把代理地址设置到非托管内存地址中
                struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
                //代理通过本地连接到代理服务器上
                struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
                bufferLength = Marshal.SizeOf(struct_IPI);
    
                //Allocating memory
                //关联到内存
                intptrStruct = Marshal.AllocCoTaskMem(bufferLength);
    
               //Converting structure to IntPtr 
               //把结构体转换到句柄
                Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
                return InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, bufferLength);
            }
            public IEProxy(string strProxy)
            {
                this.ProxyStr = strProxy;
            }
            //设置代理
            public bool SetIESettings()
            {
                return InternetSetOption(this.ProxyStr);
            }
            //取消代理
            public bool DisableIEProxy()
            {
                return InternetSetOption(string.Empty);
            }
        }
  • 相关阅读:
    使用docker部署zabbix
    进入容器脚本
    Docker安装
    docker命令总结
    zbb20180827 Spring中ClassPathXmlApplicationContext类的简单使用,main方法加载application.xml文件
    zbb20180824 spring ioc aop 例子
    tomcat运行报错Failed to start component [StandardEngine[Catalina].StandardHost[localhost].
    zbb20180815 网盘搜索引擎
    Java字节码指令收集大全
    zbb20180716 SpringBoot 使用feign时报错Service id not legal hostname
  • 原文地址:https://www.cnblogs.com/softidea/p/3314908.html
Copyright © 2011-2022 走看看