【问题标题】:Use HtmlUnit to Launch Application on Client Machine使用 HtmlUnit 在客户端机器上启动应用程序
【发布时间】:2015-12-18 21:13:34
【问题描述】:

我正在尝试通过单击使用 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”协议的支持? 如果没有,那么有什么方法可以在不点击链接的情况下获取将传递给应用程序的参数?

【问题讨论】:

    标签: java httpclient protocols htmlunit httpexception


    【解决方案1】:

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

    可以通过拦截请求获取HtmlUnit发送的请求,提示here

    以下是打印所有请求 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;
                }
            };
    

    【讨论】:

    • 感谢您的回复!当单击 jagex-jav 链接时,这似乎没有获取传递给应用程序的参数。真正的浏览器如何处理 jagex-jav 和其他类似打开的协议?我一直无法找到任何相关信息。
    【解决方案2】:

    这并不能真正回答我关于如何使 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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-09-18
      • 1970-01-01
      • 2015-05-14
      • 2020-02-22
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      相关资源
      最近更新 更多