![](/img/trans.png)
[英]I want to login to a web page by passing credentials from java application
[英]Cannot access web page with credentials from java
我正在從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:服務器返回HTTP響應代碼:401,用於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()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.