我想从网页上获取.ics文件,但我需要登录。 我可以使用以下代码登录:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://ipool.ba-berlin.de/stundenplaene.anzeige.php?faculty=15&course=6&type=html");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
HttpPost httpost = new HttpPost("http://ipool.ba-berlin.de/main.php?action=login");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("FORM_LOGIN_NAME", "MY_USERNAME"));
nvps.add(new BasicNameValuePair("FORM_LOGIN_PASS", "MY_PASSWORD"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
httpclient.getConnectionManager().shutdown();
但在我登录后,我不知道如何获取我的文件内容。我怎样才能成功呢?
非常感谢!
答案 0 :(得分:0)
如果我在登录后了解您的问题,您应该使用您的会话cookie请求资源(文件网址)。
尝试这样的事情
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", mySessionId);
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
您已设法登录,因此您应该收到回复的会话Cookie, 我在你的代码中看到它,现在你必须向你所在的url发出新的http请求,但是服务器端需要验证你已经登录了,所以它根据你收到的id在session对象中存储了关于你的数据响应为名为JSESSIONID的cookie,您需要将该cookie与新请求一起传递,该示例创建新cookie,您可以直接传递接收的cookie