首先我想万一道歉,我的问题,可以不提供足够的连接或该事项什么的,我在我的手机,现在打字了。
所以,我的工作要求我到一个网页中,为了做到这一点的任务自动化项目,第一步是访问页面摆在首位,但我已经达到了,我已经试图寻找和障碍搞清楚有没有用。
我想该网页由CloudFlare的达到了DDoS防护,进入页面之前的意思,您的浏览器查了几秒钟,然后让通过。
我使用的外部库的HtmlUnit这为我提供了一切,我需要和访问页面时,我得到一个503 error
,说我不能访问它,在相当肯定这是阻止它的保护。
现在的问题是我应该怎么绕过它。有一个.jar
我反编译,看着都到同一个站点我,但它太潦草,我做出来的。
想知道关于此任务的帮助这么多,谢谢。
作为参考,下面是一个使用CloudFare测试网页的一个例子,www.osbot.org(这是不是网站BTW)。
如果您有什么需要请让我知道,只有文本再次抱歉,这是我的手机上打字难这个了,我目前还没有PC访问。
编辑:不能白名单我的IP或与网站所有者取得联系
默认情况下,抛出的HtmlUnit异常(这不是什么是真正的浏览器一样),这是故意的。
总之,你可以使用webClient.getOptions()。setThrowExceptionOnFailingStatusCode(假)。
此外,您还需要足够wait
,下面是一个例子:
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
String url = "http://www.osbot.org/";
HtmlPage htmlPage = webClient.getPage(url);
webClient.waitForBackgroundJavaScript(10_000);
System.out.println(htmlPage.asText());
}
我知道这个问题是很老,但没有正确的答案。下面是我什么工作:
WebClient client = new WebClient(BrowserVersion.CHROME);
client.getOptions().setCssEnabled(false);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setRedirectEnabled(true);
client.getCache().setMaxSize(0);
client.waitForBackgroundJavaScript(10000);
client.setJavaScriptTimeout(10000);
client.waitForBackgroundJavaScriptStartingBefore(10000);
try {
String url = "https://www.badlion.net/";
HtmlPage page = client.getPage(url);
synchronized(page) {
page.wait(7000);
}
//Print cookies for test purposes. Comment out in production.
URL _url = new URL(url);
for(Cookie c : client.getCookies(_url)) {
System.out.println(c.getName() +"="+c.getValue());
}
//This prints the content after bypassing Cloudflare.
System.out.println(client.getPage(url).getWebResponse().getContentAsString());
} catch (FailingHttpStatusCodeException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(InterruptedException e) {
e.printStackTrace();
}
只要你试图访问的URL替换String url = "https://badlion.net/";
。
你应该问网站的所有者如果能列入白名单的IP地址。如果你正在做的事情就像试图刮掉网站,那么他们可能不会要你。