Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Target server failed to respond exception with htmlunit and tor
- HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
- public boolean retryMethod(
- final HttpMethod method,
- final IOException exception,
- int executionCount) {
- if (executionCount >= 5) {
- // Do not retry if over max retry count
- return false;
- }
- if (exception instanceof NoHttpResponseException) {
- // Retry if the server dropped connection on us
- return true;
- }
- if (!method.isRequestSent()) {
- // Retry if the request has not been sent fully or
- // if it's OK to retry methods that have been sent
- return true;
- }
- // otherwise do not retry
- return false;
- }
- };
- GetMethod httpget = new GetMethod("http://www.whatever.com/");
- httpget.getParams().
- setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
- try {
- client.executeMethod(httpget);
- System.out.println(httpget.getStatusLine().toString());
- } finally {
- httpget.releaseConnection();
- }
- WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
- client.setTimeout(60000);
- client.setRedirectEnabled(true);
- client.setJavaScriptEnabled(true);
- client.setThrowExceptionOnFailingStatusCode(false);
- client.setThrowExceptionOnScriptError(false);
- client.setCssEnabled(false);
- client.setUseInsecureSSL(true);
- HtmlPage page = null;
- try {
- page = client.getPage("http://www.whatever.com");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- }
- if (page.getWebResponse().getStatusCode() == 404) {
- System.out.println("Page not found");
- }
- // Post a request
- WebRequest request = new WebRequest(new URL("http://www.whatever.com/post_url"));
- request.setHttpMethod(HttpMethod.POST);
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new NameValuePair("login", userLogin));
- params.add(new NameValuePair("pass", userPassword));
- request.setRequestParameters(params);
- page = client.getPage(request);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement