Advertisement
Guest User

Untitled

a guest
Aug 15th, 2012
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. Target server failed to respond exception with htmlunit and tor
  2. HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
  3. public boolean retryMethod(
  4. final HttpMethod method,
  5. final IOException exception,
  6. int executionCount) {
  7. if (executionCount >= 5) {
  8. // Do not retry if over max retry count
  9. return false;
  10. }
  11. if (exception instanceof NoHttpResponseException) {
  12. // Retry if the server dropped connection on us
  13. return true;
  14. }
  15. if (!method.isRequestSent()) {
  16. // Retry if the request has not been sent fully or
  17. // if it's OK to retry methods that have been sent
  18. return true;
  19. }
  20. // otherwise do not retry
  21. return false;
  22. }
  23. };
  24.  
  25. GetMethod httpget = new GetMethod("http://www.whatever.com/");
  26. httpget.getParams().
  27. setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
  28. try {
  29. client.executeMethod(httpget);
  30. System.out.println(httpget.getStatusLine().toString());
  31. } finally {
  32. httpget.releaseConnection();
  33. }
  34.  
  35. WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
  36. client.setTimeout(60000);
  37. client.setRedirectEnabled(true);
  38. client.setJavaScriptEnabled(true);
  39. client.setThrowExceptionOnFailingStatusCode(false);
  40. client.setThrowExceptionOnScriptError(false);
  41. client.setCssEnabled(false);
  42. client.setUseInsecureSSL(true);
  43.  
  44. HtmlPage page = null;
  45. try {
  46. page = client.getPage("http://www.whatever.com");
  47. } catch (Exception e) {
  48. // TODO Auto-generated catch block
  49. }
  50. if (page.getWebResponse().getStatusCode() == 404) {
  51. System.out.println("Page not found");
  52. }
  53.  
  54. // Post a request
  55. WebRequest request = new WebRequest(new URL("http://www.whatever.com/post_url"));
  56. request.setHttpMethod(HttpMethod.POST);
  57. List<NameValuePair> params = new ArrayList<NameValuePair>();
  58. params.add(new NameValuePair("login", userLogin));
  59. params.add(new NameValuePair("pass", userPassword));
  60. request.setRequestParameters(params);
  61.  
  62. page = client.getPage(request);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement