四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2566|回复: 2
打印 上一主题 下一主题

C# WebBrowser 设置代理完全解决方案

[复制链接]
  • TA的每日心情
    开心
    2018-7-4 09:08
  • 97

    主题

    392

    帖子

    6095

    积分

    论坛元老

    Rank: 8Rank: 8

    积分
    6095
    跳转到指定楼层
    楼主
    发表于 2016-10-30 09:25:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    微软webbrowser控件也就是IE插件,他的所有功能就像IE类似,当然设置也是一样的,下面介绍下webbrowser如何设置代理,可不要用这个对抗广告联盟哦
    You can change the proxy with InternetSetOption method from the wininet.dll, here is a example to set the proxy:
    using System.Runtime.InteropServices;

    Public struct Struct_INTERNET_PROXY_INFO
    {
    public int dwAccessType;
    public IntPtr proxy;
    public IntPtr proxyBypass;
    };
    [DllImport("wininet.dll", SetLastError = true)]
    private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

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

    private void SomeFunc()
    {
    RefreshIESettings("192.168.1.200:1010");

    System.Object nullObject = 0;
    string strTemp = String.Empty;
    System.Object nullObjStr = strTemp;
    axWebBrowser1.Navigate("http://willstay.tripod.com", ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
    }
    -------------------------------------------------------------------------------------------------------------------------------------
    昨 天做的投票机遇到个新问题,昨天开始那个投票开始现在ip地址,每个地址只能投5票/天。如果每次更改ie的连接为代理服务器,那也麻烦死了,如  果改用webclient,那昨天的2个多小时就白费了,上网一通狂收还真找到了办法,这下好办了,建了一个proxy.txt文档,里面放上从网上收到  的代理服务器,然后程序读到一个listbox里面,每次需要更换ip的时候只要单击一次,就可以还一个地址重新投票了。
    附上proxy.cs

    using System.Runtime.InteropServices;//需要添加这个引用
    public struct Struct_INTERNET_PROXY_INFO
    {
    public int dwAccessType;
    public IntPtr proxy;
    public IntPtr proxyBypass;
    };
    [DllImport("wininet.dll", SetLastError = true)]
    private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
    public 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));
    }
    使用的时候,调用RefreshIESettings
    py.proxy py1 = new proxy();
    py1.RefreshIESettings(”221.4.155.51:3128″);
    System.Object nullObject = 0;
    string strTemp = String.Empty;
    System.Object nullObjStr = strTemp;
    webBrowser1.Navigate(”http://www.hfxsy.cn”, null, null, null);
  • TA的每日心情
    开心
    2018-12-28 16:25
  • 817

    主题

    1556

    帖子

    1万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    14941
    沙发
    发表于 2016-10-30 19:42:59 | 只看该作者
    新的版本默认是不使用IE代理的,就算设置WebProxy= null也不行。要到类里把request.Proxy = item.WebProxy; 这句删掉才会用IE的代理
  • TA的每日心情
    开心
    2018-12-28 16:25
  • 817

    主题

    1556

    帖子

    1万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    14941
    板凳
    发表于 2016-10-30 20:07:18 | 只看该作者
    //打开注册表键
                    Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
                    //设置代理可用
                    rk.SetValue("ProxyEnable", 1);
                    //设置代理IP和端口
                    rk.SetValue("ProxyServer", TempPoxy);
                    rk.Close();

    /// <summary>
            /// 清空ie代理
            /// </summary>
            /// <returns></returns>
            public bool SetCleanIEPoxy()
            {
                try
                {
                    //打开注册表键
                    Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
                    //设置代理不可用
                    rk.SetValue("ProxyEnable", 0);
                    rk.Close();
                    return true;
                }
                catch { return false; }
            }
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-5 13:40 , Processed in 1.109375 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表