有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Java(HttpURLConnection)对Restheart进行身份验证(对于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


共 (1) 个答案

  1. # 1 楼答案

    我做了一些更改(查找以<;==开头的内联注释),它可以工作:

    生成身份验证请求标头的方式是正确的。当我运行你的代码时,我实际上得到了415种不受支持的媒体类型,这在评论hc时就消失了。设置输出(真)。GET是一个输入操作,事实上,您还试图从连接中获取一个扩展流:实际上,您需要获取一个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);