我可以只为我想要的网络浏览器设置代理吗?

问题描述

我找到了一种在stackoverflow上的webbrowser中设置代理的方法。

How to set a proxy for Webbrowser Control without effecting the SYSTEM/IE proxy

public struct Struct_INTERNET_PROXY_INFO
{
    public int dwAccessType;
    public IntPtr proxy;
    public IntPtr proxyBypass;
};

[DllImport("wininet.dll",SetLastError = true)]
public static extern bool InternetSetOption(IntPtr hInternet,int dwOption,IntPtr lpBuffer,int lpdwBufferLength);

//to activate use like this strProxy="85.45.66.25:3633"
//to deactivate use like this strProxy=":"
public static void RefreshIESettings(string strProxy)
{
    try
    {
        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,Marshal.SizeOf(struct_IPI));
    }
    catch (Exception ex)
    {

        //TB.ErrorLog(ex);
    }
}

private void SomeFunc()
{
     RefreshIESettings("1.2.3.4:8080");
     //or RefreshIESettings("http://1.2.3.4:8080"); //both worked
     //or RefreshIESettings("http=1.2.3.4:8080"); //both worked

     System.Object nullObject = 0;
     string strTemp = "";
     System.Object nullObjStr = strTemp;
     WebBrowser1.Navigate("http://willstay.tripod.com");
}

但是,此代码是所有Web浏览器的代理设置。我的表单中有多个Web浏览器,并且我只想代理其中的一个Web浏览器。

Using proxy with WebBrowser and WebRequest,how to include username and password? 我发现了这个,但是对我没有帮助。

如果我创建一个可以连接到代理然后继承Web浏览器的自定义用户控件,可以解决此问题吗?

任何有用的信息将不胜感激

解决方法

我可以在更改为铬窗口后设置每个代理。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...