我有一个特殊的问题,即.aspx网站不会使用HttpComponents生成一个发布请求的会话ID cookie。
该网站是我学校在线时间表查看器的登录信息。 https://www.lectio.dk/lectio/22/login.aspx 或者,我将HTML发布到pastebin:http://pastebin.com/nMhxfgjL
(Brugernavn是用户名,adgangskode是密码)
我确定输入字段的名称是m $ Content $ username2和m $ Content $ password2
我的代码如下所示:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://www.lectio.dk/lectio/22/login.aspx");
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("https://www.lectio.dk/lectio/22/login.aspx");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("m$Content$username2", "blabla")); //set your own username
nvps.add(new BasicNameValuePair("m$Content$password2", "blabla")); //set your own 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());
}
}
由于显而易见的原因,我没有提供我的用户名和密码。
我从运行中获得的输出是
登录表单获取:HTTP / 1.1 200 OK
初始Cookie集:
[version:0] [name:lecmobile] [value:0] [domain:www.lectio.dk] [path:/] [expiry:Mon Dec 31 00:00:00 CET 2029]
[version:0] [name:lectiogsc] [value:831859d3-370c-a582-340d-0377177bfcae] [domain:www.lectio.dk] [路径: /] [到期时间:1月01日星期六00:59:59 CET 10000]
登录表单获取:HTTP / 1.1 200 OK
登录后的Cookie:
[version:0] [name:lecmobile] [value:0] [domain:www.lectio.dk] [path:/] [expiry:Mon Dec 31 00:00:00 CET 2029]
[version:0] [name:lectiogsc] [value:831859d3-370c-a582-340d-0377177bfcae] [domain:www.lectio.dk] [路径: /] [到期时间:1月01日星期六00:59:59 CET 10000]
唯一重要的cookie是名为“ASP.NET_SessionId”的cookie。显然,在你登录之前,你不会得到这个,至少没有价值。
另外,我可以想象HTTP响应不会是200,因为您在成功登录时被重定向...
会喜欢任何帮助:)
由于 麦克
答案 0 :(得分:0)
未获取会话cookie可能是服务器配置问题。