gpt4 book ai didi

java - 使用 HtmlUnit 在客户端计算机上启动应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:32 25 4
gpt4 key购买 nike

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

这是我的代码:

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”协议(protocol)的支持吗?如果没有,那么有什么方法可以获取将传递给应用程序的参数,而无需单击链接?

最佳答案

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

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

下面是打印所有请求 URL 及其 header 的示例。

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

关于java - 使用 HtmlUnit 在客户端计算机上启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com