import java.io.IOException;
import java.util.HashMap;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class PullHtml {
public static void main(String[] args) {
// 登陆
String url = "http://127.0.0.1/session/index.php/welcome/";
// 登陆后访问的页面
String url2 = "http://127.0.0.1/session/index.php/welcome/test";
try {
Connection conn = Jsoup.connect(url);
conn.method(Method.POST);
conn.followRedirects(false);
// 填写用户名密码
conn.data("name", "admin").data("pwd", "admin");
Response response = conn.execute();
// 获取登陆后的cookie
HashMap<String, String> cookies = (HashMap<String, String>) response.cookies();
//设置cookie访问
Document doc = Jsoup.connect(url2).cookies(cookies).get();
System.out.println(doc.body().text());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
网友评论