Two related questions, I am using the Apache HTTP Client 4.x API. myHttpPost is an instance of HttpPost, and myHttpClient is an instance of HttpClient. I am trying to send a request using basic authentication. So I have HttpClient and create HttpPost.
The brute force method for setting the basic authentication header seems to be set in the HttpPost header.
String encoding = Base64Encoder.encode("username" + ":" + "password"); myHttpPost.setHeader("Authorization", "Basic " + encoding);
The above example came from another question (now it cannot find the link). As for the Base64Encoder class - what package will I find in it or where to download it?
The main question: I was hoping to make basic authentication a more aesthetic way using the following code:
myHttpClient.getCredentialsProvider().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC), new UsernamePasswordCredentials("username", "password") );
But this does not seem to work. So, is the first example above the correct path for basic authentication with Apache HTTP Client 4.0? Or there is a cleaner / easier way.
source share