使用 Java (HttpURLConnection) 向 Restheart 进行身份验证(针对 Mongodb)

Using Java (HttpURLConnection) to authenticate to Restheart (for Mongodb)

我正在使用 restheart 为 mongodb 提供 restful 接口。如果通过 Chrome 发送 GET 请求,则接口已设置并 运行 并提供正确的答案。但是,如果我使用 HttpURLConnection 使用以下 java 代码,我会收到没有内容的 201 响应。

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

我做了一些改动(寻找以 <== 开头的内联注释)并且有效:

您生成身份验证请求的方式 header 是正确的。当我 运行 你的代码时,我实际上得到了 415 Unsupported Media Type,它消失了注释掉 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);