7

Hi I need to set proxy address of IE programmatically

Earlier I used to use this RedTomahawk.TORActivator but 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 }

Ankush Roy
  • 1,621
  • 2
  • 15
  • 25

1 Answers1

4

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");
        }
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • please provide an example :- void setproxy(string proxy,string port,string username,string password){ ///code here } – Ankush Roy Nov 24 '10 at 10:00
  • - Hi sir, I am actually using a WatIN IE for doing some requests. what happens is: after few requests my ip is being blocked. at that time I wish to change my ip address( I have some valid proxy adresses in the format xxx.xxx.xxx.xxx:xxxx:username:password). Using RedTomahawk.TORActivator I was able to set ip:port easily but now the proxy list I have asks for username password also. my question is how to set these username,passwords programatically also. Please help – Ankush Roy Nov 25 '10 at 08:38
  • What type of authentication does your proxy use? If it is basic authentication you could try setting it like this: `http://username:password@yourproxy.com:8080`. If it is integrated Windows Authentication it will depend on the user running your program. – Darin Dimitrov Nov 25 '10 at 08:49
  • Thank yoy very much Sir Darin Dimitrov for helping me. I understood that probably there is no direct way of setting the username password directly. please see this link for my actual problem http://stackoverflow.com/questions/4276044/how-to-handle-windows-security-alert-dialog-box-using-watin-ie and please help – Ankush Roy Nov 25 '10 at 10:33
  • @DarinDimitrov the code doesn't work. It returns "true" but nothing changed with the proxy settings. Could you help please? – Dark_Knight Nov 14 '14 at 12:27
  • @Dark_Knight Yes, it changes default proxy setting only for WinHTTP. According to the [documantation](https://msdn.microsoft.com/en-us/library/aa384069.aspx#_uninstall) this sets registry value `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections\WinHttpSettings`. But IE (and other browsers) uses `DefaultConnectionSettings` in same registry key. Warning: 32b browsers uses Wow6432Node branch. – Vizor May 03 '17 at 13:41