有人可以解释如何使用JSoup登录这个http://ecampus.smu.ac.kr/网站吗?
我的代码目前(完全失败,我确定我错了'轨道'):
class Insert extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
Connection.Response res = Jsoup.connect("https://ecampus.smu.ac.kr/login/index.php")
.data("username", "201490008", "password", "*******")
.method(Connection.Method.POST)
.execute();
Map<String, String> loginCookies = res.cookies();
Document doc = Jsoup.connect("http://ecampus.smu.ac.kr/").cookies(loginCookies).get();
Elements elements = doc.select(".course_name");
arrayList.clear();
for (Element element : elements) {
arrayList.add(element.text());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
答案 0 :(得分:1)
尝试使用类似的东西通过Jsoup发送POST请求并检索文档
首先:设置一个cookie管理器来处理你所有的cookie东西
CookieManager cm = new CookieManager();
CookieManager.setDefault(cm);
第二步:为你的POST参数创建一个Map<String, String>
HashMap<String, String> postMap = new HashMap<String, String>();
postMap.put("username", "xxxxxxxxxxx");
postMap.put("password", "xxxxxxxxxxxxx");
postMap.put("rememberusername", "on");
最后:设置特定标头(在我的示例中为referer和userAgent)以执行POST请求
String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36";
String referer = "http://ecampus.smu.ac.kr/";
Connection.Response res = Jsoup.connect("https://ecampus.smu.ac.kr/login/index.php")
.data(postMap)
.userAgent(userAgent)
.referrer(referer)
.method(Connection.Method.POST)
.execute();
Document doc = res.parse();
userAgent将使服务器认为它不是移动设备。这应该有用。