Here you can find the source of addBasicAuthorization(String username, String password, HttpURLConnection connection)
public static void addBasicAuthorization(String username, String password, HttpURLConnection connection)
//package com.java2s; /******************************************************************************* * Copyright (c) 2012-2016 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * /*from ww w . ja v a 2 s. c o m*/ * Contributors: * Red Hat, Inc. - initial API and implementation ******************************************************************************/ import java.net.HttpURLConnection; import javax.xml.bind.DatatypeConverter; public class Main { private static final String PROPERTY_BASIC = "Basic"; private static final String PROPERTY_AUTHORIZATION = "Authorization"; public static void addBasicAuthorization(String username, String password, HttpURLConnection connection) { String credentials = toBase64Encoded(new StringBuilder() .append(username).append(':').append(password).toString()); connection.setRequestProperty(PROPERTY_AUTHORIZATION, new StringBuilder().append(PROPERTY_BASIC).append(' ') .append(credentials).toString()); } public static String toBase64Encoded(String unencoded) { if (unencoded == null) { return null; } else if (unencoded.getBytes().length == 0) { return new String(); } return DatatypeConverter.printBase64Binary(unencoded.getBytes()); } }