我正在尝试使用 apache http 客户端(org.apache.hc.client5.http)实现跟踪器。
在第一部分中,我需要发出请求 GET 关闭 FOLLOW REDIRECT。为此,请使用以下代码:
try (CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(cm)
.disableRedirectHandling()
.build()) {
URI uri = new URI("https://MINHA URL");
HttpGet httpget = new HttpGet(uri);
String location;
String cookie;
try (CloseableHttpResponse response = httpclient.execute(httpget)) {
location = response.getFirstHeader("Location");
cookie = response.getFirstHeader("Set-Cookie");
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
爬虫的第二部分必须对位置返回的 URL 执行请求 POST,并设置 cookie。但是,对于这个请求,我需要客户端遵循重定向。
如何创建客户端使用的代码禁用了重定向,如下disableRedirectHandling中所示:
CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(this.cm)
.disableRedirectHandling()
.build()
我的问题是我应该如何启用和禁用 disableRedirectHandling?
在 CloseableHttpClient 类中,我还没有确定允许我执行此操作的方法。
我应该如何在 HTTP 客户端上进行这些修改?