Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.Base64; public class JavaHttpURLConnectionsetAuthenticatorExample1 { //Starting the main method... public static void main(String h[]) throws IOException { //starting the try block try { String link_url ; link_url="https://www.javatpoint.com"; URL url = new URL(link_url); URLConnection uc = url.openConnection(); String password = "123"; String username = "Javatpoint"; String userpass = username + ":" + password; System.out.println("https://www.javatpoint.com"); String basicAuth = "Basic :" + new String(Base64.getEncoder().encode(userpass.getBytes())); System.out.println(basicAuth); uc.setRequestProperty ("Authorization", basicAuth); } catch (Exception e) { System.out.println(e); } } }
Output