ContentGrabbingJi/ContentGrabbingJi-pixiv/src/main/java/net/lamgc/pixiv/BasicPixivSession.java
LamGC 2bea395cf7
[Add] Pixiv 添加一组与 Pixiv 登录会话相关的类;
[Add] PixivLogin, PixivLoginException 初版的登录器接口;
[Add] PixivSession, BasicPixivSession Pixiv 帐号登录会话抽象类和一个默认基本实现;
[Add] ObjectInputStreamLogin 通过反序列化 CacheStore 进行登录的登录器;
2020-10-07 14:14:16 +08:00

71 lines
2.3 KiB
Java

/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.pixiv;
import com.google.common.net.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import java.io.IOException;
/**
* 基本的 Pixiv 会话对象.
* @author LamGC
*/
public class BasicPixivSession extends PixivSession {
private final static String PIXIV_LOGOUT_URL = "https://www.pixiv.net/logout.php";
private final HttpClient httpClient;
private boolean login = true;
/**
* 构造一个会话对象.
* @param cookieStore 会话 Cookies.
* @param httpClient 用于请求登出的 HttpClient 对象.
*/
public BasicPixivSession(CookieStore cookieStore, HttpClient httpClient) {
super(cookieStore);
this.httpClient = httpClient;
}
@Override
public boolean logOut() throws IOException {
HttpGet request = new HttpGet(PIXIV_LOGOUT_URL);
StringBuilder builder = new StringBuilder();
getCookies().getCookies().forEach(cookie ->
builder.append(cookie.getName()).append("=").append(cookie.getValue()).append("; "));
request.addHeader(HttpHeaders.COOKIE, builder.toString());
HttpResponse response = httpClient.execute(request);
final int httpRedirectCode = 302;
if (response.getStatusLine().getStatusCode() == httpRedirectCode) {
login = false;
return true;
}
return false;
}
@Override
public boolean isLogin() {
return login;
}
}