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

问题描述

我找到了一种在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浏览器的自定义用户控件,可以解决此问题吗?

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

解决方法

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