At least version 4.2.3 (I think after version 3.X), the accepted answer is no longer valid. Instead, do something like:
private HttpClient createClient() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setContentCharset(params, "UTF-8"); Credentials credentials = new UsernamePasswordCredentials("user", "password"); DefaultHttpClient httpclient = new DefaultHttpClient(params); httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials); return httpclient; }
The JavaDoc for AuthScope.ANY
says that future versions of HttpClient will stop using this parameter, so use it at your own risk. The best option would be to use one of the constructors defined in AuthScope
.
For a discussion of how to prioritize queries, see:
Proactive Basic Authentication with Apache HttpClient 4
source share