[英]401 error while trying to connect to a Https url from Java
I tried to access a https url using the HttpsURLConnection class with the username and password(base64 encoded) passed to it.我尝试使用 HttpsURLConnection 类访问 https url,并将用户名和密码(base64 编码)传递给它。
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.但是,当我将用户名、加密的pwd
和url
以 json 格式存储在 DB 中并尝试获取它们并连接到它时,会引发 401 错误。 Printing values from DB show that the values are correct.从 DB 打印值表明这些值是正确的。
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(dbjson);
String url= jsonObject.get("url").toString().replace('"', ' ').trim();
这是通过使用 org.json.JSONObject 读取字符串 JSON 和 .getString("") 来检索值来修复的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.