Configuration 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
.
For web browsers, you must use IP address authentication, then configure your network proxy settings. If you use Firefox or Chrome, the FoxyProxy plugin makes it really easy to configure your proxy settings.
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,
specifically version 2.7.0 and greater. 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,
and you should definitely omit the auth
parameter
and use IP authentication for https requests.
>>> import requests >>> proxies = {'http': 'http://USERNAME:PASSWORD@PROXYHOST:PORT', 'https': 'http://USERNAME:PASSWORD@PROXYHOST:PORT'} >>> response = requests.get('http://example.com', proxies=proxies)
Scrapy
For the scrapy crawling framework,
you must set the http_proxy
environment variable, as shown above, then
activate
the HttpProxyMiddleware.
Ruby
There are a couple good options for Ruby http clients. You can leave out the username and password parts if you are using IP authentication. Here's an example for HTTParty.
Using HTTParty HTTParty.get(url, { http_proxyaddr: "PROXYHOST", http_proxyport: "PORT", http_proxyuser: "USERNAME", http_proxypass: "PASSWORD", :timeout => 3 })
Another library you can use is Typhoeus.
Using typhoeus options = {proxy: 'http://PROXYHOST:PORT', proxyuserpwd: 'USERNAME:PASSWORD'} req = Typhoeus::Request.new(url, options) req.run
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')}"
PHP
For PHP, you can use the included
Client URL Library (cURL).
To configure proxy settings, use the curl_setopt
function to set CURLOPT_PROXY
to the proxy host and port,
such as PROXYHOST:PORT
.
Optionally, for authentication, you can set CURLOPT_PROXYUSERPWD
to
USERNAME:PASSWORD
.
Java
There are many Java HTTP client that you can use. Here's a link to examples of using the Commons HTTP Client with Proxies. Example code is included below:
HttpClient client = new HttpClient(); HttpConnectionManager conManager = client.getHttpConnectionManager(); client.getHostConfiguration().setProxy("PROXYHOST", PORT); HttpState state = new HttpState(); state.setProxyCredentials(null, null, new UsernamePasswordCredentials("USERNAME", "PASSWORD")); client.setState(state);
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;
ASP VBScript
<% Const HTTPREQUEST_PROXYSETTING_PROXY = 2 Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1 Dim responseText With Server.CreateObject("WinHttp.WinHttpRequest.5.1") .SetProxy HTTPREQUEST_PROXYSETTING_PROXY, "PROXYHOST:PORT" .Open "GET", "http://example.com" .SetCredentials "USERNAME", "PASSWORD", HTTPREQUEST_SETCREDENTIALS_FOR_PROXY .Send responseText = .ResponseText End With %>