Answer the question
In order to leave comments, you need to log in
How to issue https authentication request for Github api?
With curl, this can be done with the following command: curl -u username https://api.github.com/user.
The one written below throws a FileNotFoundException at the line with the declaration of the InputStream. the server returns 401 (Unauthorized).
public static void makeAuthRequest(URL url) throws IOException {
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
try {
connection.setRequestProperty("Authorization", "Basic " +
Base64.encode("мой_ник:мой_пароль".getBytes(), Base64.NO_WRAP));
InputStream content = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
StringBuilder sb = new StringBuilder("");
while ((line = in.readLine()) != null) {
sb.append(line + '\n');
}
System.out.println(sb.toString());
} catch (IOException ioex) {
ioex.printStackTrace();
} finally {
connection.disconnect();
}
}
Answer the question
In order to leave comments, you need to log in
Here is a working version:
public static void makeAuthRequest(URL url) throws IOException {
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
String userpass = "login" + ":" + "password";
String basicAuth = "Basic " + String.valueOf(Base64.encode(userpass.getBytes(), Base64.DEFAULT));
uc.setRequestProperty("Authorization", basicAuth);
InputStreamReader inputStreamReader = new InputStreamReader(uc.getInputStream());
Scanner sc = new Scanner(inputStreamReader);
sc.useDelimiter("\\A");
if (sc.hasNext()) {
String str = sc.next();
Log.d("TAG", str);
}
uc.disconnect();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question