POST数据和获取cookie
登录该站点时,可能会设置一个授权会话cookie,需要在后续请求中发送以维护会话。
可以像这样获取cookie:
Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
.data("username", "myUsername", "password", "myPassword")
.method(Method.POST)
.execute();
Document doc = res.parse();
String sessionId = res.cookie("SESSIONID"); //需要检查正确的cookie名称
下一个请求发送cookie数据:
Document doc2 = Jsoup.connect("http://www.example.com/otherPage")
.cookie("SESSIONID", sessionId)
.get();
GET请求数据:
Document doc = Jsoup.connect(url).get();
Elements divElements = doc.getElementsByTag("div");
for(Element divElement : divElements){
if(divElement.attr("class")
.equals("animal-info"))
{
textList.add(divElement.text());
text = textList.toString();
Log.e("Content", text);
}
}
或
public void JsoupGet() throws Exception {
Connection connect = Jsoup.connect(
"http://www.itcast.cn?username=zhangsan&password=lisi");
// 带参数开始
connect.data("username", "zhangsan");
connect.data("password", "lisi");
// 带参数结束
Document document = connect.get();
System.out.println(document.toString());
}