Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Fixed a cookie exception
Browse files Browse the repository at this point in the history
  • Loading branch information
aowubulao committed Jul 21, 2020
1 parent 15ac6ba commit 88e7a56
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/main/java/com/neoniou/daily/request/LoginRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
public class LoginRequest {

private static final int OK = 200;

private static String lt;

public static String login(String username, String password) {
Expand All @@ -28,26 +30,33 @@ public static String login(String username, String password) {
.header("Cookie", "org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=zh_CN")
.execute();

lt = getLt(indexRes.body());
Map<String, Object> loginMap = generateLoginMap(username, password);
List<HttpCookie> cookies;
if (OK == indexRes.getStatus()) {
lt = getLt(indexRes.body());
Map<String, Object> loginMap = generateLoginMap(username, password);

// 替换登录 url
List<HttpCookie> cookies = indexRes.getCookies();
String sessionId = cookies.get(0).toString();
String loginUrl = DailyApi.SWU_LOGIN.replace("sessionId", sessionId);
// 替换登录 url
cookies = indexRes.getCookies();
String sessionId = cookies.get(0).toString();
String loginUrl = DailyApi.SWU_LOGIN.replace("sessionId", sessionId);

// 登录步骤 1
HttpResponse loginRes = HttpRequest.post(loginUrl)
.cookie(cookies.toString())
.form(loginMap)
.execute();
cookies = loginRes.getCookies();
// 登录步骤 1
HttpResponse loginRes = HttpRequest.post(loginUrl)
.cookie(cookies.toString())
.form(loginMap)
.execute();
cookies = loginRes.getCookies();

// 跳转最终获取 Cookie
HttpResponse moveRes = HttpRequest.get(loginRes.header("Location"))
.cookie(cookies.toString())
.execute();
cookies = moveRes.getCookies();
// 跳转最终获取 Cookie
HttpResponse moveRes = HttpRequest.get(loginRes.header("Location"))
.cookie(cookies.toString())
.execute();
cookies = moveRes.getCookies();
} else {
HttpResponse moveRes = HttpRequest.get(indexRes.header("Location"))
.execute();
cookies = moveRes.getCookies();
}

return handleCookie(cookies);
}
Expand Down

0 comments on commit 88e7a56

Please sign in to comment.