1

我正在尝试通过单击使用 HtmlUnit 的网页上的按钮来启动应用程序。url 包含 HttpClient 不支持的协议。该协议使当您单击链接时,它会通过传递某些参数在您的计算机上启动应用程序。

这是我的代码:

public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
    WebClient webClient = new WebClient();
    webClient.getOptions().setJavaScriptEnabled(false);
    System.out.println("Downloading DarkScape webpage...");
    HtmlPage page = webClient.getPage("http://darkscape.runescape.com/game");
    System.out.println("Finding play button...");
    DomElement playButton = page.getElementById("yes-play-now");
    System.out.println("Clicking play button...");
    try{
        page = playButton.click();
        System.out.println("Launching DarkScape!");
    }catch(Exception e){
        System.out.println("Failed :(");
        e.printStackTrace();
    }
    webClient.closeAllWindows();
}

错误:

java.lang.RuntimeException: org.apache.http.client.ClientProtocolException
at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2067)
at com.gargoylesoftware.htmlunit.html.HtmlAnchor.doClickStateUpdate(HtmlAnchor.java:125)
at com.gargoylesoftware.htmlunit.html.HtmlAnchor.doClickStateUpdate(HtmlAnchor.java:162)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:786)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:733)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:680)
at me.jaylynn.darkscapelauncher.DarkScapeLauncher.main(DarkScapeLauncher.java:22)

Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:179)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1321)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1238)
at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2063)
... 6 more

Caused by: org.apache.http.HttpException: jagex-jav protocol is not supported
at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:88)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183)
... 11 more

有什么方法可以从 HtmlUnit 添加对“jagex-jav”协议的支持?如果没有,那么有什么方法可以获取将传递给应用程序的参数而无需单击链接?

4

2 回答 2

0

这并不能真正回答我关于如何使 HtmlUnit 支持 jagex-jav 的问题,但它确实提供了一种解决方法。我没有单击按钮,而是获得了 href 并使用 Desktop 打开 uri。

if(Desktop.isDesktopSupported()){
        try{
            String href = playButton.getAttribute("href");
            Desktop.getDesktop().browse(new URI(href));
            System.out.println("Launching DarkScape!");
        }catch(Exception e){
            System.out.println("Failed :(");
            e.printStackTrace();
        }
    }
于 2015-09-22T12:43:27.803 回答
0

jagex-jav实际浏览器不支持,因此它超出了 HtmlUnit 的范围。

您可以通过拦截请求来获取 HtmlUnit 发送的请求,如此所示。

下面是一个打印所有请求 URL 及其标头的示例。

        new WebConnectionWrapper(webClient) {

            public WebResponse getResponse(WebRequest request) throws IOException {
                System.out.println(request.getUrl());
                System.out.println(request.getAdditionalHeaders());
                WebResponse response = super.getResponse(request);
                return response;
            }
        };
于 2015-09-22T07:00:01.800 回答