在 Java 中使用 Zimbra 获取 Calendar REST Api

Consume Zimbra Get Calendar REST Api in Java

我在 Android 上使用 Zimbra 的 REST Api 时出现此错误:必须 Athenticate java.lang.RuntimeException:失败:HTTP 错误代码:401 在 ZimbraREST.main(ZimbraREST.java:33)。但是我绝对确定我的登录名和密码是正确的,昨天我的代码运行良好,而且我没有修改任何与此代码相关的重要内容。这段代码应该给我一个 xml 文件,我用它来在我的应用程序上获取同步日历。谢谢你的帮助。这是我的代码:

@Override
protected Void doInBackground(Integer... integers) {
    System.out.println("15email_adress=" + email_adress);
    System.out.println("15password=" + password);
    String dayStart = String.valueOf(integers[0]);
    String monthStart = String.valueOf(integers[1]);
    String yearStart = String.valueOf(integers[2]);
    String dayEnd = String.valueOf(integers[3]);
    String monthEnd = String.valueOf(integers[4]);
    String yearEnd = String.valueOf(integers[5]);
    String[] strings = {dayStart, monthStart, yearStart, dayEnd, monthEnd, yearEnd};

    int k = 0;
    for(String s: strings){
        if(s.length() < 2){
            strings[k] = "0" + s;
        }
        k++;
    }

    dayStart = strings[0];
    monthStart = strings[1];
    yearStart = strings[2];
    dayEnd = strings[3];
    monthEnd = strings[4];
    yearEnd = strings[5];

    try {
        URL url = new URL("https://zmail.insa-lyon.fr/home/" + email_adress + "/Calendrier%20Cocktail?fmt=xml&start=" +
                monthStart +
                "/" +
                dayStart +
                "/" +
                yearStart +
                "&end=" +
                monthEnd +
                "/" +
                dayEnd +
                "/" +
                yearEnd);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(email_adress, password.toCharArray());
            }
        });
        conn.setRequestMethod("GET");
        conn.setRequestProperty("--user", email_adress + ":" + password);
        if (conn.getResponseCode() != 200) {
            System.out.println("15erreur=" + url.toString());
            System.out.println("15erreur=" + conn.getResponseMessage());
            System.out.println("15erreur=" + conn.getRequestMethod());
            return null;
        }
        System.out.println(conn.getResponseMessage());
        InputStreamReader inputStreamReader = new InputStreamReader((conn.getInputStream()));
        BufferedReader br = new BufferedReader(inputStreamReader);
        String output;

        StringBuilder xml = new StringBuilder();
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            xml.append(output);
        }
        inputStreamReader.close();
        br.close();
        conn.disconnect();
        System.out.println(xml);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xml.toString())));
        rootElement = document.getDocumentElement();
    } catch (IOException | ParserConfigurationException | SAXException e) {
        e.printStackTrace();
    }
    return null;
}

好的,所以我必须使用以下代码对用户进行身份验证:

        // Authentication part -------------------
        String userpass = email_adress + ":" + password;
        conn.setRequestProperty("X-Requested-With", "Curl");
        String basicAuth = "Basic " + new String(android.util.Base64.encode(userpass.getBytes(), android.util.Base64.DEFAULT));
        conn.setRequestProperty("Authorization", basicAuth);
        // ----------------------------------------