0
我正在使用restheart爲mongodb提供一個寧靜的界面。如果通過Chrome發送GET請求,該界面將設置並運行並提供正確的答案。但是,如果我使用下面的Java代碼使用HttpURLConnection,我得到一個沒有內容的201響應。使用Java(HttpURLConnection)對Restheart進行身份驗證(適用於Mongodb)
try {
videos = new URL("http://www.example.com:8080/myflix/videos");
} catch (Exception et) {
System.out.println("Videos URL is broken");
return null;
}
HttpURLConnection hc = null;
try {
hc = (HttpURLConnection) videos.openConnection();
String login="admin:admin";
final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
final String encoded = Base64.getEncoder().encodeToString(authBytes);
hc.addRequestProperty("Authorization", "Basic "+encoded);
hc.setDoInput(true);
hc.setDoOutput(true);
hc.setUseCaches(false);
hc.setRequestMethod("GET");
hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
hc.setRequestProperty("Content-Type", "application/json");
hc.setRequestProperty("Accept", "application/json,text/html,application/hal+json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*");
} catch (Exception et) {
System.out.println("Can't prepare http URL con");
return (null);
}
BufferedReader br = null;
try {
OutputStreamWriter writer = new OutputStreamWriter(
hc.getOutputStream());
} catch (Exception et) {
System.out.println("Can't get reader to videos stream");
}
String inputLine;
String sJSON = null;
try {
int rc = hc.getResponseCode();
什麼是使用Java對resthert接口進行身份驗證的正確方法? (上restheart認證細節是這裏Restheart authentication
是完美的作品,除去hc.setDoOutput(真);問題解決了我也做了415錯誤,我設法通過添加hc.setRequestProperty(「Content-Type」,「application/hal + json」)來讓它們消失。 –