Aem 如何从独立代码调用CQ作者URL

Aem 如何从独立代码调用CQ作者URL,aem,sling,Aem,Sling,我试图从我的独立代码中点击cq Author实例中的URL。URL看起来像-http://:///libs/dam/gui/content/reports/export.json 代码如下: URL url = new URL(newPath); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRe

我试图从我的独立代码中点击cq Author实例中的URL。URL看起来像-
http://:///libs/dam/gui/content/reports/export.json

代码如下:

URL url = new URL(newPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");
connection.setReadTimeout(15 * 10000);
connection.connect();

reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
但我得到了一个401错误,这是意料之中的,因为我没有传递任何身份验证信息-因此Sling说:

getAnonymousResolver:配置请求凭据不允许匿名访问


如何解决此问题?

您可以使用基本HTTP身份验证。将其添加到
HttpURLConnection
有点尴尬:

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("admin", "admin".toCharArray());
    }
});
考虑使用:

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
DefaultHttpClient authorizedClient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(url);
request.addHeader(new BasicScheme().authenticate(creds, request));
HttpResponse response = authorizedClient.execute(request);
InputStream stream = response.getEntity().getContent();