jsoup 登录及获取页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
private Map<String, String> getCookies() throws Exception {
//获取 Cookie
Connection.Response res = Jsoup.connect(baseLoginUrl)
.method(Method.GET)
.execute();
Map<String, String> sessionId = res.cookies();
return sessionId;
}
private void postOK(String url,String param,Map<String, String> cookies) throws Exception {
OkHttpClient client = new OkHttpClient();
String cookiesString = getCookiesString(cookies);
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, param);
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
.addHeader("Cookie", cookiesString)
.build();
com.squareup.okhttp.Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
Map<String, String> sessionId = getCookies();
//get
Response execute1 = Jsoup.connect(liulanliang.get(0))
.header("X-Requested-With","XMLHttpRequest")
.ignoreContentType(true).cookies(sessionId).execute();
String url = "";
String param = "{}";
postOK(url, param, sessionId);