I tried hitting my server using HttpsURLConnection
, got response code 401 - need to authenticate. Tried the following:
Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication ("myuser", "!mypassword".toCharArray());
}
});
Got response code 400 - bad request.
Based on some stackoverflow research I also tried using:
String authorizationString = "Basic " + Base64.encodeToString(("username" + ":" + "password").getBytes(), Base64.DEFAULT);
conn.setRequestProperty("Authorization", authorizationString);
Same problem, code 400. I'm also trusting all hosts using the @Chrispix method in this post: Trust Anchor not found for Android SSL Connection not sure if that is interfering somehow, but this is for a prototype and the site certificate isn't valid, so I needed to bypass the check.
The connection is set up with these properties, not sure if there is anything wrong or missing:
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.addRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoInput(true);
Thanks!