[英]Logging into a website?( Jsoup)
使用Jsoup登錄網站需要什么? 我相信我的代碼是正確的,但是我從未使用Jsoup成功登錄到網站,因此我可能會遺漏一些東西。 這是我的代碼:
try {
String url = ("http://quadrigacx.com/login");
Connection.Response loginForm = (Connection.Response)Jsoup.connect(url)
.method(Connection.Method.GET)
.execute();
Document loginDoc = loginForm.parse();
Elements loginElements = loginDoc.select("input:not([name=client_id]):not([name=password])");
int i = 0;
String v[] = new String[loginElements.size()];
for (Element element: loginElements){
v[i++] = element.attr("value");
}
int ii = 0;
String n[] = new String[loginElements.size()];
for (Element element: loginElements){
n[ii++] = element.attr("name");
}
Connection.Response loginFormLogin = (Connection.Response)Jsoup.connect(url)
.cookies(loginForm.cookies())
.data(n[0],v[0])
.data("client_id", "xxxxxxx")
.data("password", "xxxxxx")
.data(n[1],v[1])
.data(n[2],v[2])
.method(Connection.Method.POST)
.execute();
Document document2 = Jsoup.connect("http://quadrigacx.com/settings")
.cookies(loginFormLogin.cookies())
.get();
System.out.print(document2.toString());
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
document2
返回一個登錄頁面,顯示該頁面未成功登錄。有一個名為time
的輸入值,我認為這可能是為什么它不起作用的原因。 這個值會隨着時間的推移而上升。 我運行了兩次代碼,時間變量返回了1511226860
和1511226876
。 我的代碼需要大約10秒鍾來打印文檔,所以time
變量在發送發布請求時已經改變了? 我不確定這是否是問題。 也許還有其他我沒看到的東西? 謝謝。
編輯:這是代碼,我已經使用用戶ID和密碼登錄后發布了身份驗證。 loginCookies
是第一次登錄時的cookie。 Connection.Response auth = Jsoup.connect("https://quadrigacx.com/authenticate") .userAgent("Mozilla") .method(Connection.Method.POST) .cookies(loginCookies) .data("google_code", googleCode.getText()) .data("email_code", emailCode.getText()) .data("authenticate", "Authenticate") .followRedirects(true) .execute();
我也嘗試過: byte[] gcText = googleCode.getText().getBytes(ISO_8859_1); String gcValue = new String(gcText, UTF_8); byte[] ecText = emailCode.getText().getBytes(ISO_8859_1); String ecValue = new String(ecText, UTF_8); Connection.Response auth = Jsoup.connect("https://quadrigacx.com/authenticate") .userAgent("Mozilla") .method(Connection.Method.POST) .cookies(loginCookies) .data("google_code", gcValue) .data("email_code", ecValue) .data("authenticate", "Authenticate") .followRedirects(true) .execute();
byte[] gcText = googleCode.getText().getBytes(ISO_8859_1); String gcValue = new String(gcText, UTF_8); byte[] ecText = emailCode.getText().getBytes(ISO_8859_1); String ecValue = new String(ecText, UTF_8); Connection.Response auth = Jsoup.connect("https://quadrigacx.com/authenticate") .userAgent("Mozilla") .method(Connection.Method.POST) .cookies(loginCookies) .data("google_code", gcValue) .data("email_code", ecValue) .data("authenticate", "Authenticate") .followRedirects(true) .execute();
您需要在請求中添加更多參數: csrf,時間,哈希
空表代碼:
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class Quadrigacx {
public static String[] TABLE = new String[]{}; // Add data here
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
final String URL = "https://www.quadrigacx.com/login/";
String password = "password";
String clientId = "id";
String hashPassword = getHash(new String[]{clientId, password});
Connection.Response response = Jsoup.connect(URL)
.userAgent("Mozilla")
.method(Connection.Method.GET)
.execute();
Element csrf = response.parse().select("input[name=csrf]").first();
Element time = response.parse().select("input[name=time]").first();
Element hash = response.parse().select("input[name=hash]").first();
Jsoup.connect(URL)
.userAgent("Mozilla")
.method(Connection.Method.POST)
.cookies(response.cookies())
.data("password", hashPassword)
.data("client_id", clientId)
.data("csrf", csrf.attr("value"))
.data("time", time.attr("value"))
.data("hash", hash.attr("value"))
.execute();
String googleCode = "";
while (!googleCode.matches("^(?=[0-9]+)\\d{6}$")) {
System.out.print("Please enter the Two-Factor Authentication to validate your login. (Numbers Only): ");
Scanner in = new Scanner(System.in);
googleCode = in.nextLine();
}
Jsoup.connect("https://www.quadrigacx.com/authenticate")
.userAgent("Mozilla")
.method(Connection.Method.POST)
.cookies(response.cookies())
.data("google_code", googleCode)
.data("redirect", "dash")
.data("authenticate", "Authenticate")
.execute();
response = Jsoup.connect("https://www.quadrigacx.com/dash/")
.userAgent("Mozilla")
.cookies(response.cookies())
.method(Connection.Method.GET)
.execute();
System.out.println(response.body());
}
private static String getHash(String[] loginArray) throws NoSuchAlgorithmException, UnsupportedEncodingException {
StringBuilder h = new StringBuilder();
for (String data : loginArray)
h.append(data).append(getSalt(data));
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] byteHash = digest.digest(h.toString().getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder(byteHash.length * 2);
for (byte b : byteHash)
sb.append(String.format("%02x", b));
return sb.toString();
}
private static String getSalt(String arg) throws UnsupportedEncodingException {
return TABLE[getLastByte(arg)];
}
private static int getLastByte(String login) throws UnsupportedEncodingException {
final byte[] utf8Bytes = login.getBytes("UTF-8");
return utf8Bytes[utf8Bytes.length - 1];
}
}
完整的工作代碼,您可以在這里找到(很長):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.