By default DotNetBrowser uses system proxy settings. These default settings are the same proxy settings that MS Internet Explorer uses. You can configure Browser instance to use its own proxy settings. When you modify Browser proxy settings you don't modify the system global proxy settings.

Since DotNetBrowser 1.8.3 it is possible to modify proxy settings for a BrowserContext instance that has been already created. This can be done by simply assigning another ProxyConfig object to the Browser.Context.ProxyConfig property. New proxy settings will be applied immediately.

browser.Context.ProxyConfig = new CustomProxyConfig(proxyRules, exceptions);


Custom/Manual

To configure Browser instance to use custom proxy settings use the following approach:

String proxyRules = "http=foo:80;https=foo:80;ftp=foo:80;socks=foo:80";
String exceptions = "<local>";  // bypass proxy server for local web pages
contextParams.ProxyConfig = new CustomProxyConfig(proxyRules, exceptions);

// Creates Browser instance with context configured to use specified proxy settings.
Browser browser = BrowserFactory.Create(new BrowserContext(contextParams));

This code configures Browser instance to use custom proxy server addresses (foo:80) for HTTP, HTTPS, FTP, and SOCKS protocols. Proxy settings will be ignored for local web pages (<local>).


Other examples of the proxyRules string:  

"http=foopy:80;ftp=foopy2"

use HTTP proxy "foopy:80" for http:// URLs, and HTTP proxy "foopy2:80" for ftp:// URLs.  

"foopy:80"

use HTTP proxy "foopy:80" for all URLs. 

"socks4://foopy"

use SOCKS v4 proxy "foopy:1080" for all URLs. 


The format of the exceptions string can be any of the following: 

  • [ URL_SCHEME "://" ] HOSTNAME_PATTERN [ ":" <port> ] Examples: "foobar.com", "*foobar.com", "*.foobar.com", "*foobar.com:99", "https://x.*.y.com:99"  
  • "." HOSTNAME_SUFFIX_PATTERN [ ":" PORT ] Examples: ".google.com", ".com", "http://.google.com"  
  • [ SCHEME "://" ] IP_LITERAL [ ":" PORT ] Examples: "127.0.1", "[0:0::1]", "[::1]", "http://[::1]:99"  
  • IP_LITERAL "/" PREFIX_LENGHT_IN_BITS Examples: "192.168.1.1/16", "fefe:13::abc/33"  
  • "<local>" Match local addresses. The meaning of "<local>" is whether the host matches one of: "127.0.0.1", "::1", "localhost".  

If you need to provide several exception rules you can separate them using comma: "*foobar.com,.google.com,<local>". 


AutoDetect

Browser will automatically detect proxy settings.

BrowserContextParams contextParams = new BrowserContextParams(dataDir);

// Browser will automatically detect proxy settings.
contextParams.ProxyConfig = new AutoDetectProxyConfig();

// Creates Browser instance with context configured to use specified proxy settings.
Browser browser = BrowserFactory.Create(new BrowserContext(contextParams));

Direct

Browser will not use a proxy server.

BrowserContextParams contextParams = new BrowserContextParams(dataDir);

// Browser will not use a proxy server.
contextParams.ProxyConfig = new DirectProxyConfig();

// Creates Browser instance with context configured to use specified proxy settings.
Browser browser = BrowserFactory.Create(new BrowserContext(contextParams));

PAC File

Browser will use proxy settings received from proxy auto-config (PAC) file.

BrowserContextParams contextParams = new BrowserContextParams(dataDir);

// Browser will use proxy settings received from proxy auto-config (PAC) file.
contextParams.ProxyConfig = new URLProxyConfig("<pac-file-url>");

// Creates Browser instance with context configured to use specified proxy settings.
Browser browser = BrowserFactory.Create(new BrowserContext(contextParams));

Proxy Authentication

Proxy server might require authorization. In this case user must provide valid login/password to be able to use proxy server. DotNetBrowser provides API that allows handling proxy server authentication where you can provide login/password programmatically without any UI dialogs or your can display your own login dialog where you can ask end user for login/password. The following sample code demonstrates how to handle proxy authorization and provide login/password programmatically without displaying login dialog:

BrowserContextParams contextParams = new BrowserContextParams(dataDir);

// Browser will use custom user's proxy settings.
String proxyRules = "http=foo:80;https=foo:80;ftp=foo:80;socks=foo:80";
String exceptions = "<local>";  // bypass proxy server for local web pages
contextParams.ProxyConfig = new CustomProxyConfig(proxyRules, exceptions);

// Creates Browser instance with context configured to use specified proxy settings.
Browser browser = BrowserFactory.Create(new BrowserContext(contextParams));

// Register proxy server authentication handler
browser.Context.NetworkService.NetworkDelegate = new MyNetworkDelegate();

Implementation of the MyNetworkDelegate is the following:

class MyNetworkDelegate : NetworkDelegate
{
    public bool OnAuthRequired(AuthRequiredParams parameters)
    {
        // Authentication is required for proxy server.
        if (parameters.IsProxy)
        {
            // Provides login & password for proxy server.
            parameters.Username = "login";
            parameters.Password = "password";
        }
        // Don't cancel authentication
        return false;
    }
}