2015-10-30 42 views
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

回答

2

我做了一些改動(請注意開頭爲< ==在線評論),以及它的工作原理:

你生成認證請求頭是正確的方式當我運行你的代碼我實際上得到了415不支持的媒體類型,這就消失了註釋掉hc.setDoOutput(true)。GET是一個輸入操作,實際上你也試圖從連接中獲得一個OutStream:你需要實際得到一個InputStream 。

URL url; 

    try { 
     url = new URL("http://127.0.0.1:8080/test/huge"); 
    } catch (Exception et) { 
     System.out.println("Videos URL is broken"); 
     Assert.fail(et.getMessage()); 
     return; 
    } 

    HttpURLConnection hc = null; 

    try { 
     hc = (HttpURLConnection) url.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); 

     System.out.println("Authorization: " + hc.getRequestProperty("Authorization")); 

     hc.setDoInput(true); 
     //hc.setDoOutput(true); <== removed, otherwise 415 unsupported media type 
     hc.setUseCaches(false); 

     hc.setRequestMethod("GET"); 
     hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch"); 
     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"); 
    } 

    System.out.println(hc.toString()); 

    BufferedReader br = null; 

    try { 
     InputStreamReader reader = new InputStreamReader(hc.getInputStream()); // <== the request is a GET, data is in input 
    } catch (Exception et) { 
     System.out.println("Can't get reader to videos stream"); 
    } 

    int rc = hc.getResponseCode(); 

    System.out.println("response code: " + rc); 
    System.out.println("response message: " + hc.getResponseMessage()); 

    Assert.assertEquals(200, rc); 
+0

是完美的作品,除去hc.setDoOutput(真);問題解決了我也做了415錯誤,我設法通過添加hc.setRequestProperty(「Content-Type」,「application/hal + json」)來讓它們消失。 –

相關問題