I tried to access a https url using the HttpsURLConnection class with the username and password(base64 encoded) passed to it.
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Basic "+userPassword);
It worked correctly when the username and password were hardcoded in the code.
However, when I stored the username, encrypted pwd
, and url
in json format in DB and tried to fetch them and connect to it, a 401 error is thrown. Printing values from DB show that the values are correct.
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(dbjson);
String url= jsonObject.get("url").toString().replace('"', ' ').trim();
THis was fixed by using org.json.JSONObject to read the String JSON and .getString("") to retrieve the values.