开发者

Programmatically Set Proxy Address, Port, Username, Password

开发者 https://www.devze.com 2023-01-26 10:52 出处:网络
Hi I need to set proxy address of IE programmatically Earlier I used to use this RedTomahawk.TORActivator

Hi I need to set proxy address of IE programmatically

Earlier I used to use this RedTomahawk.TORActivator but开发者_StackOverflow it doesnot gives an option to set those proxies which requires username and password.

How can we set those proxies which needs username and passwords

Please provide examples like

void Setproxy(string ip,string port,string uname,string pwd) { ///Code here }


You could P/Invoke the WinHttpSetDefaultProxyConfiguration function.


UPDATE:

Including example as requested:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WINHTTP_PROXY_INFO
{
    public AccessType AccessType;
    public string Proxy;
    public string Bypass;
}

public enum AccessType
{
    DefaultProxy = 0,
    NamedProxy = 3,
    NoProxy = 1
}

class Program
{
    [DllImport("winhttp.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern bool WinHttpSetDefaultProxyConfiguration(ref WINHTTP_PROXY_INFO config);

    static void Main()
    {
        var config = new WINHTTP_PROXY_INFO();
        config.AccessType = AccessType.NamedProxy;
        config.Proxy = "http://proxy.yourcompany.com:8080";
        config.Bypass = "intranet.com";

        var result = WinHttpSetDefaultProxyConfiguration(ref config);
        if (!result)
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        else
        {
            Console.WriteLine("Successfully modified proxy settings");
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消