简体   繁体   中英

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 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();  

这是通过使用 org.json.JSONObject 读取字符串 JSON 和 .getString("") 来检索值来修复的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM