My This code works with Java6 but not with Java7. I used both HtmlUnit2.12 and HtmlUnit2.17, nothing works. Please suggest what I am missing here?
From Java7 I am getting this Exception : java.net.SocketTimeoutException: Read timed out
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class Test {
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
String url = "http : / / kellyserviceshk . force . com / careers";
String content = fetchPage(url, "firefox", 30000, true);
System.out.println(content);
}
private static String fetchPage(String url, String browser, long delayinmillis, boolean javaScriptEnable) {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.getOptions().setJavaScriptEnabled(javaScriptEnable);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
String content="";
try {
HtmlPage page = webClient.getPage(url);
webClient.waitForBackgroundJavaScript(delayinmillis);
content = page.asXml();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(webClient!=null){
webClient.closeAllWindows();
webClient=null;
}
}
return content;
}
}
While changing HtmlUnit2.17 I Used BrowserVersion as FIREFOX_38, not FIREFOX_17. What changes should I make?
You can try to define the timeout:
The default timeout is 90 seconds (it was 0 up to HtmlUnit-2.11).