1

How to set the default proxy for Internet Explorer in C# .net?

I wanna make a browser capable of searching for a valid proxy in a .txt file and using it.
The IP and Port used with the set default proxy command are valid however, the command itself does absolutely nothing.

The only way I've found to use a proxy with a VS2013 .net browser is by adding it manually to IE which is incredibly useless.

class Proxy
{
    List<string> Proxy_IP;
    List<int> Proxy_Port;

    public Proxy()
    {
        Proxy_IP = new List<string>();
        Proxy_Port = new List<int>();
        populateProxyList();
    }

    public void findProxy()
    {
        for (sbyte i = 0; i < Proxy_IP.Count; i++)
        {
            string IP = Proxy_IP[i];
            int Port = Proxy_Port[i];

            if (isValid(IP, Port))
            {
                setDefaultProxy(IP, Port);
                break;
            }
        }
    }

    private void populateProxyList()
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\Proxies.txt");
        foreach (string line in lines)
        {
            int endIP = line.IndexOf('#');
            int startPort = endIP + 1;
            int portLength = line.Length - (endIP + 2);

            string IP = line.Remove(endIP);
            int Port = Convert.ToInt32(line.Substring(startPort, portLength));

            Proxy_IP.Add(IP);
            Proxy_Port.Add(Port);
        }
    }

    private void setDefaultProxy(string ip, int port)
    {
        System.Windows.Forms.MessageBox.Show(ip + ":" + port.ToString()); // Proxy is valid
        System.Net.WebRequest.DefaultWebProxy = new WebProxy(ip, port); // Doesn't do shit
    }

    private bool isValid(string IP, int Port)
    {
        bool pingable = false;
        Ping pinger = new Ping();

        try
        {
            PingReply reply = pinger.Send(IP);
            pingable = reply.Status == IPStatus.Success;
        }
        catch (PingException)
        {

        }
        return pingable;
    }

2 Answers2

1

As said in MSDN DefaultWebProxy page http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy%28v=vs.110%29.aspx,

The DefaultWebProxy property reads proxy settings from the app.config file. If there is no config file, the current user's Internet Explorer (IE) proxy settings are used.

So if your problem is about setting a proxy only for browsing inside Visual Studio (2013 for you), you should add proxy info inside the config file of Visual Studio, named "devenv.exe.config" :

<system.net>
    <defaultProxy useDefaultCredentials=“true“ enabled=“true“>
        <proxy bypassonlocal=”true” proxyaddress=”http://yourproxyaddress.net:proxyPort” />
    </defaultProxy>
</system.net>

See this article for more details: http://en.code-bude.net/2013/07/15/how-to-setup-a-proxy-server-in-visual-studio-2012/

If your problem is about setting a proxy for all your browsers (ie: defaut internet proxy), you should use PInvoke and change the registry settings: see response here How to change Global Windows Proxy using C# .NET with `Immediate Effect`

Community
  • 1
  • 1
Nicolas R
  • 13,812
  • 2
  • 28
  • 57
  • Is there a way to set it programmatically? Setting it manually before executing the program would take too much time. – user3130362 Dec 26 '13 at 10:15
  • You need to change your proxy parameters frequently (like using proxy at a client site, not at home)? For that kind of case, I prefer setting the proxy in my Internet Options (not modifying the configuration file of VS) using a software like "IEProxy" which allow you to switch proxies quickly. It must use the same lines of code of my 2nd solution behind I think Setting the proxy inside the config file must not be done at every launch if your location is always the same, it's one shot – Nicolas R Dec 26 '13 at 10:18
  • Can you post again your last comment? I have the beginning in my inbox but cannot see it in this page. Did you delete it? – Nicolas R Dec 26 '13 at 10:44
1
   WebProxy webProxy = (WebProxy) WebRequest.DefaultWebProxy;
   if (webProxy.Address.AbsoluteUri != string.Empty)
   {
       Console.WriteLine("Proxy URL: " + webProxy.Address.AbsoluteUri);
   }
Yanshof
  • 9,659
  • 21
  • 95
  • 195