【问题标题】:How to cancel getPage in Htmlunit?如何在 Htmlunit 中取消 getPage?
【发布时间】:2017-08-21 21:45:40
【问题描述】:

在 htmlunit 中有没有办法用 TimeOut 以外的线程取消 getPage?

我试过Thread.stop,但是这个方法不方便。

【问题讨论】:

    标签: java htmlunit


    【解决方案1】:

    我已经尝试并为我工作的是,在调用 getPage() 方法(从 WebClient 对象)之前,创建一个线程,在其构造函数中传递您将用于 getPage 的相同 webClient 对象。然后启动线程,然后调用 getPage() 方法。在线程内部,您应该调用 webClient.close(),它会中止 getPage 方法,但您将不得不处理 getPage() 方法的一些异常。

    主线程代码:

    WebClient webClient;
    
    /* Initialize and configure webClient */
    /* ... */
    
    YourThread yourThread = new YourThread(webClient);
    yourThread.start();
    
    try {
        webClient.getPage(someRequest);
    } catch (IllegalStateException e) {
        /* Ignore, it is caused because of aborting getPage */
    }
    

    你的线程代码:

    private WebClient webClient;
    
    public YourThread(WebClient webClient) {
        this.webClient = webClient;
    }
    
    @Override
    public void run() {
        webClient.close();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多