很简单,举例如下:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.setConnectTimeout(10 * 1000);
urlConnection.setReadTimeout(10 * 1000);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
String username = "[username]";
String password = "[password]";
String input = username + ":" + password;
String encodingInput = Base64.encodeToString(input.getBytes(), 0); urlConnection.setRequestProperty("Authorization", "Basic " + encodingInput);
urlConnection.connect();