gpt4 book ai didi

java - 无法使用 java 的凭据访问网页

转载 作者:行者123 更新时间:2023-12-01 11:18:53 25 4
gpt4 key购买 nike

我正在从 java 代码访问 RabbitMQ 队列信息。

    public class NewClass {
private static Object Base64Converter;
public static void main(String args[])
{
try {

String credentials = "test" + ":" + "test";
String encoding = base64Encode(credentials);
URL url = new URL("http://192.168.0.30:15672/api/queues");
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", String.format("Basic %s", encoding));

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;

while ((inputLine = in.readLine()) != null) {
// Process each line.
System.out.println(inputLine);
}
in.close();

} catch (MalformedURLException me) {
System.out.println(me);

} catch (IOException ioe) {
System.out.println(ioe);
}
}
private static String base64Encode(String stringToEncode)
{
return DatatypeConverter.printBase64Binary(stringToEncode.getBytes());
}

java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.30:15672/api/queues

最佳答案

您准备了一个具有正确身份验证的 URLConnection,但在调用 url.openStream() 时并未使用它。这应该有效:

 ...
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", String.format("Basic %s", encoding));
uc.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));

关于java - 无法使用 java 的凭据访问网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31491069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com