[英]How to get web page that contain errors with HtmlUnit?
我正在尝试使用HtmlUnit 2.15 API在Java程序中访问此Ajax 页面 ,但是在尝试获取页面时失败。 我认为原因是网站对此位于此处的损坏/丢失文件的要求。
我的代码:
public class HtmlUnitExample {
public static void main(String[] args) throws Exception, FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
webClient.getOptions().setTimeout(120000);
webClient.waitForBackgroundJavaScript(60000);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setDoNotTrackEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
String url = "http://www.santanderuniversidades.com.br/JuriPopular/index.aspx?idprojeto=16";
final HtmlPage page = (HtmlPage) webClient.getPage(url); //Fails here
System.out.println(page.asXml());
}
}
错误信息:
Exception in thread "main" java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:116)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.http.client.entity.LazyDecompressingInputStream.read(LazyDecompressingInputStream.java:68)
at com.gargoylesoftware.htmlunit.HttpWebConnection.downloadContent(HttpWebConnection.java:693)
at com.gargoylesoftware.htmlunit.HttpWebConnection.downloadResponseBody(HttpWebConnection.java:675)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:201)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1313)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1230)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:338)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:407)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:392)
at HtmlUnitExample.main(HtmlUnitExample.java:42)//getPage line
页面链接到CSS :
<link href='/JuriPopular/App_Themes/estilo/css.axd?files=jPages.css,estilo.css,jquery.fancybox.css' type='text/css' rel='stylesheet' />
调用缺少的字体文件的CSS:
@font-face{font-family:'DigitalDotRoadsign';
src:url('fonts/DigitalDotRoadsign.eot');
src:url('fonts/DigitalDotRoadsign.eot?#iefix') format('embedded-opentype'),
url('fonts/DigitalDotRoadsign.woff') format('woff'), //call missing file
url('fonts/DigitalDotRoadsign.ttf') format('truetype'),
url('fonts/DigitalDotRoadsign.svg#svgDigitalDotRoadsign') format('svg');
font-weight:normal;
}
这是我问题的根源吗? 如果是这样,有什么办法可以避免吗? 也许忽略/消除问题的原因?
为了解决这个问题,我只是启用了cookie。 我想有必要加载页面。
编码:
webClient.getCookieManager().setCookiesEnabled(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.