我有一个特殊的问题,即 .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 集:
[版本:0][名称:lecmobile][值:0][域:www.lectio.dk][路径:/][到期:2029 年 12 月 31 日星期一 00:00:00 CET]
[版本:0][名称:lectiogsc][值:831859d3-370c-a582-340d-0377177bfcae][域:www.lectio.dk][路径:/][到期:1 月 1 日星期六 00:59:59 CET 10000 ]
登录表单获取:HTTP/1.1 200 OK
登录后 cookie:
[版本:0][名称:lecmobile][值:0][域:www.lectio.dk][路径:/][到期:2029 年 12 月 31 日星期一 00:00:00 CET]
[版本:0][名称:lectiogsc][值:831859d3-370c-a582-340d-0377177bfcae][域:www.lectio.dk][路径:/][到期:1 月 1 日星期六 00:59:59 CET 10000 ]
唯一重要的 cookie 是一个名为“ASP.NET_SessionId”的 cookie。显然,在您登录之前,您不会得到这个,至少不会得到这个值。
另外,我可以想象 HTTP 响应不会是 200,因为您在成功登录后被重定向......
希望有任何帮助:)
谢谢迈克。