如何以编程方式登录网站?
示例:用户在EditText中输入他的登录数据,然后单击登录按钮。然后打开一个WebView,他在那里登录。
我尝试使用这些代码示例但是:
How to connect via HTTPS using Jsoup? - 我有错误
How to log in to an HTTPS website with Jsoup? - 我没有错误,但我无法登录
Jsoup HTTPS connecting - 它不起作用我得错误
在我使用它之后,我尝试了这个
class JsoupAsyncIserv extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
//HTML Parsen
try {
Connection.Response login = Jsoup.connect("https://lmg-nhs.de/idesk")
.data("login_act", "MyUsername")
.data("login_pwd", "MyPW")
.method(Connection.Method.POST)
.execute();
Document document = Jsoup.connect("https://lmg-nhs.de/idesk")
.cookies(login.cookies())
.get();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
它有效,但我无法登录。
private class IServAsyncTask extends AsyncTask<Void, Void, Void>{
private String resp;
protected String doInBackground(String... params) {
Connection.Response res = null;
try {
res = Jsoup.connect("https://lmg-nhs.de")
.data("login_act", "myUsername")
.data("login_pwd", "myPW")
.data("submit", "Anmelden")
.method(Connection.Method.POST)
.execute();
} catch (IOException e) {
e.printStackTrace();
}
try {
Document doc = res.parse();
} catch (IOException e) {
e.printStackTrace();
}
String sessionId = res.cookie("PHPSESSID");
try {
Document doc2 = Jsoup.connect("https://lmg-nhs.de")
.cookie("PHPSESSID", sessionId)
.get();
} catch (IOException e) {
e.printStackTrace();
}
return resp;
}
@Override
protected Void doInBackground(Void... voids) {
return null;
}
}
如果可以的话,尝试在一个或者我的例子中解释它,因为初学者(像我一样)可以更容易地学习它。
答案 0 :(得分:1)
试试这个:
Document doc2 = Jsoup.connect("https://lmg-nhs.de")
.cookie(res.cookies())
.get();
而不是
Document doc2 = Jsoup.connect("https://lmg-nhs.de")
.cookie("PHPSESSID", sessionId)
.get();
在此示例中,我们将使用GitHub类登录FormElement网站。
// # Constants used in this example
final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
final String LOGIN_FORM_URL = "https://github.com/login";
final String USERNAME = "yourUsername";
final String PASSWORD = "yourPassword";
// # Go to login page
Connection.Response loginFormResponse = Jsoup.connect(LOGIN_FORM_URL)
.method(Connection.Method.GET)
.userAgent(USER_AGENT)
.execute();
// # Fill the login form
// ## Find the form first...
FormElement loginForm = (FormElement)loginFormResponse.parse()
.select("div#login > form").first();
checkElement("Login Form", loginForm);
// ## ... then "type" the username ...
Element loginField = loginForm.select("#login_field").first();
checkElement("Login Field", loginField);
loginField.val(USERNAME);
// ## ... and "type" the password
Element passwordField = loginForm.select("#password").first();
checkElement("Password Field", passwordField);
passwordField.val(PASSWORD);
// # Now send the form for login
Connection.Response loginActionResponse = loginForm.submit()
.cookies(loginFormResponse.cookies())
.userAgent(USER_AGENT)
.execute();
System.out.println(loginActionResponse.parse().html());
public static void checkElement(String name, Element elem) {
if (elem == null) {
throw new RuntimeException("Unable to find " + name);
}
}
所有表单数据都由FormElement类为我们处理(甚至是表单方法检测)。调用Connection方法时会生成现成的FormElement#submit。我们所要做的就是用附加标题(cookies,用户代理等)完成这个连接并执行它。