我需要从其他站点获取某些莲花连接站点的数据,例如用户的状态。我尝试通过java建立与莲花的连接,例如
> server = "https://" + path + param + "&format=full";
> URL profiles_url = new URL(server);
> // Open the URL: throws exception if not found
> HttpURLConnection profiles_conn = HttpURLConnection)profiles_url.openConnection();
> profiles_conn.connect();
> // Process the Atom feed in the response content
> readResponse(profiles_url.openStream(),args[0]);
但我总是得到响应:HTTP / 1.1 401 Unauthorized 请给我任何建议?
答案 0 :(得分:1)
我用这种方式解决了身份验证问题:
protected void doView(RenderRequest rRequest, RenderResponse rResponse) throws PortletException, IOException, UnavailableException {
try {
rResponse.setContentType("text/html");
URL url = new URL(
"https://xxx/activities/service/atom2/todos");
URLConnection con = url.openConnection();
con.setConnectTimeout(150000);
con.setReadTimeout(150000);
writeCookies(con, rRequest);
DO_SOMETHING (con.getInputStream());
} catch (Exception ex) {
ex.printStackTrace();
}
}
private String doesLTPATokenCookieExists(RenderRequest request) {
Cookie[] cookie = request.getCookies();
for (int i = 0; i < cookie.length; i++) {
System.out.println("Cookie Name " + cookie[i].getName());
if (cookie[i].getName().equals("LtpaToken"))
return cookie[i].getValue();
}
return null;
}
public URLConnection writeCookies(URLConnection urlConn,
RenderRequest request) {
String cookieString = "";
cookieString += "LtpaToken" + "=" + doesLTPATokenCookieExists(request) + "; ";
urlConn.setRequestProperty("Cookie", cookieString);
return urlConn;
}
答案 1 :(得分:0)
您没有提到您的身份验证方式,这一点至关重要。正如401错误所暗示的那样,Connections不会将您的请求视为已通过身份验证。你需要一个有效的Authenticator
实例,但是你的代码片段表明你没有这样做,对吗?
(另外,在使用Lotus Connections API时,建议使用Apache Abdera项目。)