简体   繁体   English

尝试从 Java 连接到 Https url 时出现 401 错误

[英]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.但是,当我将用户名、加密的pwdurl以 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.

相关问题 尝试连接到URL以读取xml时出现错误401 - Error 401 while trying to connect to URL for reading xml 试图从谷歌网址获取HTML文本,但错误401 - Trying to get html texts from google url, but error 401 尝试将 Java 与数据库连接时出错 - Error while trying to connect Java with database 从 Java 中的 URL 读取 JSON 避免 401 错误 - Reading JSON from a URL in Java avoiding 401 error 从 wsdl url 生成类 java - 返回错误 401 - Generate classes java from a wsdl url - returning error 401 Java-在尝试调用WebService时,Got Server返回了URL:****的HTTP响应代码:401 - Java - Got Server returned HTTP responce code: 401 for URL: **** while trying to call a WebService 尝试使用 AWS SDK 从 Java 应用程序连接 SQS 时出现 403 禁止错误 - 403 forbidden error while trying to connect SQS from Java application using AWS SDK 尝试使用 Java 连接到 mySQL 服务器(工作台)时出错 - error while trying to connect to mySQL server (workbench) with Java 获取错误401尝试使用play.libs.OAuth连接到Twitter - Getting error 401 trying to connect to Twitter with play.libs.OAuth 从Java连接到https网址 - Connecting to a https url from java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM