Home » Java » Where can I find the Base64Encoder class?

Where can I find the Base64Encoder class?

Posted by: admin December 28, 2021 Leave a comment


Hi I saw an example on StackOverflow which had this code:

String encoding = Base64Encoder.encode ("test1:test1");
HttpPost httppost = new HttpPost("http://host:post/test/login");
httppost.setHeader("Authorization", "Basic " + encoding);

System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

I have a task similar to this. I tried to use the same code but I couldn’t find the Base64Encoder class anywhere. Im using httpclient-4.1.2.jar and httpcore-4.1.2.jar. Can someone help me with this?


A Base64 en/decoder has long been missing from the Java APIs, so you’ll have to find a good 3rd party one, e.g. Apache Commons Codec, iHarder Base64, etc.


I think you need to have commons codec also to use Base64 encoding with HttpClient. Here is link. Commons codec link


Since maerics asked me to:

Since I think Java6 there is actually the DatatypeConverter class which is part of JAXB.

The two static public methods parseBase64Binary and printBase64Binary should be of interest. Also note it’s defined in java.lang so it’s an official part of the JDK. You can use it, but personally I like the commons variant better since it doesn’t return a string when decoding the base64 stuff..