I would like to connect some domain with java code. I can connect the domain in the browser as follow: http://username:[email protected]
I tryed the following:
String enc = "username" + ":" + "password";
String encoded = new sun.misc.BASE64Encoder().encode(loginPassword.getBytes());
URL url = new URL("domain.com");
URLConnection conn = url.openConnection();
conn.setRequestProperty ("Authorization", "Basic " + encoded);
I get the error: 401/Unutorized... java.lang.NoClassDefFoundError: sun/misc/BASE64Encoder
Is there any solution I can try?.
Oracle has announced the removal of those classes in Java 9.
You should not be using classes that are in sun.*
packages - those classes are not part of the public API of Java and can change in any new Java version.
See Why Developers Should Not Write Programs That Call 'sun' Packages in Oracle's documentation.
Instead of using class sun.misc.BASE64Encoder
:
If you are using Java 8, then use class java.util.Base64
for Base 64 encoding and decoding.
Otherwise, use a third-party library, for example class org.apache.commons.codec.binary.Base64
from Apache Commons Codec.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With