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
Python urllib
and urllib2 will both use
the http_proxy environment variable. But if you want to configure
proxy use manually, here is an example for urllib2:
>>> import urllib2
>>> proxy_handler = urllib2.ProxyHandler({'http': 'http://PROXYHOST:PORT/'})
>>> proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
>>> proxy_auth_handler.add_password('REALM', 'PROXYHOST', 'USERNAME', 'PASSWORD')
>>> opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
>>> opener.open('http://example.com/')
Scrapy
For the scrapy crawling framework,
you must set the http_proxy environment variable, as shown above, then
activate
the HttpProxyMiddleware.
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;