java - 目标服务器无法响应 htmlunit 和 tor 异常

标签 java htmlunit tor

我正在使用 htmlunit 向网站和 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();
}

但是我找不到如何在 htmlunit 中执行此操作。我怎样才能实现这个目标?

最佳答案

提供的代码是一个HttpClient示例,如果你想使用htmlunit,去他们的网站查看http://htmlunit.sourceforge.net/通过这些片段,您应该能够发送(发布?)请求

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);

关于java - 目标服务器无法响应 htmlunit 和 tor 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11289907/

相关文章:

java - 我的 for 循环出了什么问题?

java - MP4Parser 空指针异常

java - 在java中列表末尾的链表中移动所有现有元音

java - 使用htmlunit获取HTML页面

java - htmlUnit - 如何获取非元素内容

python - Selenium WebDriver + Tor 作为 Stem 的代理?

java - Querydsl 更新子句填充跳过 bean 空值属性

java - html单元 : An invalid or illegal selector was specified

android - 使用 ProcessBuilder 运行 tor 二进制文件

c - 与 libevent2 配对的 bufferevents 的问题