Configure examples for HTTP Basic Proxy Authentication

Below are examples showing how to configure the Proxy Host with Basic Authentication in various HTTP clients.
Many Unix/Linux clients support the http_proxy environment variable, or even rely on it exclusively. For those clients, do the following:
export http_proxy=http://USERNAME:PASSWORD@PROXYHOST:PORT

ProxyMesh also supports IP address authentication, in which case you do not need to use a username and password in the examples below.
Instead, set the the http_proxy environment variable to http://PROXYHOST:PORT.

cURL

cURL understands the http_proxy environment variable, but if it is not set, you can also do the following:

curl -x http://PROXYHOST:PORT -U USERNAME:PASSWORD http://example.com

Wget

Wget will use the http_proxy environment variable if it is set. The example below shows how to use wget if http_proxy is not set.

http_proxy="http://PROXYHOST:PORT" wget --proxy-user=USERNAME --proxy-password=PASSWORD http://example.com

Python

The best Python library for doing HTTP requests is Requests. The example below shows the most reliable way to use proxy authentication, but you can omit the auth parameter if you're using IP authentication.

>>> import requests
>>> auth = requests.auth.HTTPProxyAuth('USERNAME', 'PASSWORD')
>>> proxies = {'http': 'http://PROXYHOST:PORT'}
>>> response = requests.get('http://example.com', proxies=proxies, auth=auth)

Scrapy

For the scrapy crawling framework, you must set the http_proxy environment variable, as shown above, then activate the HttpProxyMiddleware.

PhantomJS and CasperJS

PhantomJS has a --proxy setting for specifying the proxy host:port. It also has a --proxy-auth setting that unfortunately doesn't work. So if you want to use username:password authentication, you must do the following in your code:

> page.customHeaders={'Proxy-Authorization': 'Basic '+btoa('USERNAME:PASSWORD')};

If you're using CasperJS then the syntax is slightly different:

casper.page.customHeaders = 'Proxy-Authorization': "Basic #{btoa('USERNAME:PASSWORD')}"

C# .Net

The following example code for C# .Net was provided by Michael Eisenstein:

WebProxy ProxyString = new WebProxy("http://PROXYHOST:PORT", true);
//set network credentials may be optional
NetworkCredential proxyCredential = new NetworkCredential("USERNAME", "PASSWORD");
ProxyString.Credentials = proxyCredential;
WebRequest.DefaultWebProxy =  ProxyString;

HttpWebRequest request = (HttpWebRequest);
//manually set authorization header
string authInfo = "USERNAME" + ":" + "PASSWORD";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Proxy-Authorization"] = "Basic " + authInfo;