add Site.disableCookieManagement #577
parent
49de9374cd
commit
636359300f
|
@ -41,6 +41,8 @@ public class Site {
|
||||||
|
|
||||||
private boolean useGzip = true;
|
private boolean useGzip = true;
|
||||||
|
|
||||||
|
private boolean disableCookieManagement = false;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
DEFAULT_STATUS_CODE_SET.add(HttpConstant.StatusCode.CODE_200);
|
DEFAULT_STATUS_CODE_SET.add(HttpConstant.StatusCode.CODE_200);
|
||||||
}
|
}
|
||||||
|
@ -309,6 +311,21 @@ public class Site {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isDisableCookieManagement() {
|
||||||
|
return disableCookieManagement;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloader is supposed to store response cookie.
|
||||||
|
* Disable it to ignore all cookie fields and stay clean.
|
||||||
|
* Warning: Set cookie will still NOT work if disableCookieManagement is true.
|
||||||
|
* @param disableCookieManagement disableCookieManagement
|
||||||
|
*/
|
||||||
|
public Site setDisableCookieManagement(boolean disableCookieManagement) {
|
||||||
|
this.disableCookieManagement = disableCookieManagement;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Task toTask() {
|
public Task toTask() {
|
||||||
return new Task() {
|
return new Task() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -138,6 +138,6 @@ public class HttpClientDownloader extends AbstractDownloader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getHtmlCharset(HttpResponse httpResponse, byte[] contentBytes) throws IOException {
|
private String getHtmlCharset(HttpResponse httpResponse, byte[] contentBytes) throws IOException {
|
||||||
return CharsetUtils.detectCharset(httpResponse.getEntity().getContentType().getValue(), contentBytes);
|
return CharsetUtils.detectCharset(httpResponse.getEntity().getContentType() == null ? "" : httpResponse.getEntity().getContentType().getValue(), contentBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,6 +127,10 @@ public class HttpClientGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateCookie(HttpClientBuilder httpClientBuilder, Site site) {
|
private void generateCookie(HttpClientBuilder httpClientBuilder, Site site) {
|
||||||
|
if (site.isDisableCookieManagement()) {
|
||||||
|
httpClientBuilder.disableCookieManagement();
|
||||||
|
return;
|
||||||
|
}
|
||||||
CookieStore cookieStore = new BasicCookieStore();
|
CookieStore cookieStore = new BasicCookieStore();
|
||||||
for (Map.Entry<String, String> cookieEntry : site.getCookies().entrySet()) {
|
for (Map.Entry<String, String> cookieEntry : site.getCookies().entrySet()) {
|
||||||
BasicClientCookie cookie = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue());
|
BasicClientCookie cookie = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue());
|
||||||
|
|
|
@ -172,6 +172,23 @@ public class HttpClientDownloaderTest {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test_set_request_header() throws Exception {
|
public void test_set_request_header() throws Exception {
|
||||||
HttpServer server = httpServer(13423);
|
HttpServer server = httpServer(13423);
|
||||||
|
|
Loading…
Reference in New Issue