【问题标题】:curl and Java program are treated differently by CouchDBCouchDB 对 curl 和 Java 程序的处理方式不同
【发布时间】:2021-05-29 16:41:52
【问题描述】:

这个问题可能有点幼稚……

我编写了一个小的 Java 程序,它应该查询一个 CouchDB 实例。但是,CouchDB 不断返回我的用户未获得授权。使用与 curl 相同的 URL 是可行的。

Java 代码:

        HttpGet request = new HttpGet("http://admin:PASSWORD@localhost:5984/ecm_ng_nonpart/_security");
        request.addHeader("accept", "application/xml");

        try {
            System.out.println("Executing request " + request.getMethod() + " " + request.getUri());
            ClassicHttpResponse resp = httpClient.execute(request);
            BufferedReader br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

回应:

{"error":"unauthorized","reason":"You are not authorized to access this db."}

卷曲输出:

C:\Users\joche>curl -X GET http://admin:PASSWORD@localhost:5984/ecm_ng_nonpart/_security
{"members":{"roles":["_admin"],"names":["ecmnp","admin"]},"admins":{"roles":["_admin"],"names":["admin"]}}

知道我做错了什么吗?

【问题讨论】:

  • 您确定使用的是相同的凭据吗?在上面的java代码中你有密码“PASSWORD”但是在curl代码中你有密码“admin123”
  • 感谢您的提示。我使用了正确的密码。
  • 也许 curl 会自动应用基本身份验证,而您的 Java 代码不会。你可以使用wireshark来检查。

标签: java couchdb


【解决方案1】:

使用与 curl 相同的 URL 有效。

您发布的信息不支持此声明。在这两种情况下,您的网址是不同的。

Java:_sessions

  HttpGet request = new HttpGet("http://admin:PASSWORD@localhost:5984/ecm_ng_nonpart/_sessions");

卷曲:_security。

curl -X GET http://admin:PASSWORD@localhost:5984/ecm_ng_nonpart/_security

【讨论】:

  • 对!适当的工作为时已晚。无论哪种方式,_security 都适用于 curl,但不适用于 Java。
  • 好吧,我们只能回答实际提出的问题:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多