use of us.codecraft.webmagic.Page in project webmagic by code4craft.
the class AbstractDownloader method addToCycleRetry.
protected Page addToCycleRetry(Request request, Site site) {
Page page = new Page();
Object cycleTriedTimesObject = request.getExtra(Request.CYCLE_TRIED_TIMES);
if (cycleTriedTimesObject == null) {
page.addTargetRequest(request.setPriority(0).putExtra(Request.CYCLE_TRIED_TIMES, 1));
} else {
int cycleTriedTimes = (Integer) cycleTriedTimesObject;
cycleTriedTimes++;
if (cycleTriedTimes >= site.getCycleRetryTimes()) {
return null;
}
page.addTargetRequest(request.setPriority(0).putExtra(Request.CYCLE_TRIED_TIMES, cycleTriedTimes));
}
page.setNeedCycleRetry(true);
return page;
}
use of us.codecraft.webmagic.Page in project webmagic by code4craft.
the class HttpClientDownloaderTest method testCycleTriedTimes.
@Test
public void testCycleTriedTimes() {
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
Task task = Site.me().setDomain("localhost").setCycleRetryTimes(5).toTask();
Request request = new Request(PAGE_ALWAYS_NOT_EXISTS);
Page page = httpClientDownloader.download(request, task);
assertThat(page.getTargetRequests().size() > 0);
assertThat((Integer) page.getTargetRequests().get(0).getExtra(Request.CYCLE_TRIED_TIMES)).isEqualTo(1);
page = httpClientDownloader.download(page.getTargetRequests().get(0), task);
assertThat((Integer) page.getTargetRequests().get(0).getExtra(Request.CYCLE_TRIED_TIMES)).isEqualTo(2);
}
use of us.codecraft.webmagic.Page in project webmagic by code4craft.
the class HttpClientDownloaderTest method test_set_request_cookie.
@Test
public void test_set_request_cookie() throws Exception {
HttpServer server = httpServer(13423);
server.get(eq(cookie("cookie"), "cookie-webmagic")).response("ok");
Runner.running(server, new Runnable() {
@Override
public void run() throws Exception {
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
Request request = new Request();
request.setUrl("http://127.0.0.1:13423");
request.addCookie("cookie", "cookie-webmagic");
Page page = httpClientDownloader.download(request, Site.me().toTask());
assertThat(page.getRawText()).isEqualTo("ok");
}
});
}
use of us.codecraft.webmagic.Page in project webmagic by code4craft.
the class HttpClientDownloaderTest method test_set_site_cookie.
@Test
public void test_set_site_cookie() throws Exception {
HttpServer server = httpServer(13423);
server.get(eq(cookie("cookie"), "cookie-webmagic")).response("ok");
Runner.running(server, new Runnable() {
@Override
public void run() throws Exception {
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
Request request = new Request();
request.setUrl("http://127.0.0.1:13423");
Site site = Site.me().addCookie("cookie", "cookie-webmagic").setDomain("127.0.0.1");
Page page = httpClientDownloader.download(request, site.toTask());
assertThat(page.getRawText()).isEqualTo("ok");
}
});
}
use of us.codecraft.webmagic.Page in project webmagic by code4craft.
the class HttpClientDownloaderTest method test_disableCookieManagement.
@Test
public void test_disableCookieManagement() throws Exception {
HttpServer server = httpServer(13423);
server.get(not(eq(cookie("cookie"), "cookie-webmagic"))).response("ok");
Runner.running(server, new Runnable() {
@Override
public void run() throws Exception {
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
Request request = new Request();
request.setUrl("http://127.0.0.1:13423");
request.addCookie("cookie", "cookie-webmagic");
Page page = httpClientDownloader.download(request, Site.me().setDisableCookieManagement(true).toTask());
assertThat(page.getRawText()).isEqualTo("ok");
}
});
}
Aggregations