我们已经尝试了大量的 Java Web 实用程序,当我们尝试发布到 Quora 登录 URL 时,它们都返回 500。我们使用 Chrome 网络选项卡来查找标头/请求内容,并且所有内容都应该设置得很好。我们不确定为什么每次尝试发送登录请求时它都会返回 500 服务器错误。
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
public static void test(String email, String password) throws IOException {
// Login first
String loginFormUrl = "https://quora.com/login";
String loginActionUrl = "https://www.quora.com/webnode2/server_call_POST";
Connection.Response loginForm = Jsoup.connect(loginFormUrl).userAgent(USER_AGENT).method(Connection.Method.GET).execute(); // removed useragent
// "formkey": "f3e47dd339ed5673c9411822ad5ff1da", length = 32
String[] key = loginForm.body().split("formkey\": \"");
String[] wat = key[1].split("\",");
String formkey = wat[0];
// windowId, length = 27
String[] parts = loginForm.body().split("windowId = \"");
String[] another = parts[1].split("\";");
String windowId = another[0];
// Vcon
String[] vcon1 = loginForm.body().split(Pattern.quote("new(require(\"login\").LoggedOutHomeHeaderInlineLogin"));
String[] vcon2 = vcon1[1].split("\",\"");
vcon2[0] = vcon2[0].replace(")(\"", "");
String vcon_json = "["+vcon2[0]+"]";
// Cookies
String result = "";
boolean first = true;
for(String c : loginForm.cookies().keySet()){
String value = loginForm.cookies().get(c);
if(first){
result = result + c + "=" + value;
first = false;
}
else{
result = result + "; " + c + "=" + value;
}
}
Connection.Response loginPage = Jsoup.connect(loginActionUrl)
.data("json", "{\"args\":[],\"kwargs\":{\"email\":\""+email+"\",\"password\":\""+password+"\",\"passwordless\":0}}")
.data("formkey", formkey)
.data("window_id", windowId)
.data("__vcon_json", vcon_json)
.data("__vcon_method", "do_login")
.header("Accept", "application/json, text/javascript, **; q=0.01")
.header("Accept-Encoding", "gzip, deflate, br")
.header("Accept-Language", "en-US,en;q=0.8")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Connection", "keep-alive")
.header("Content-Length", loginForm.body().length()+"")
.header("Cookie", result)
.header("Host", "www.quora.com")
.header("Origin", "https://www.quora.com")
.header("Referer", "https://www.quora.com/")
.header("User-Agent", USER_AGENT)
.header("X-Requested-With", "XMLHttpRequest")
.userAgent(USER_AGENT)
.cookies(loginForm.cookies())
.followRedirects(true)
.referrer("https://www.quora.com/")
.method(Method.POST)
.execute();
System.out.println(loginPage.statusCode());
}