gpt4 book ai didi

java - 在 Web 服务中使用 htmlunit 时抛出的异常是什么意思?

转载 作者:行者123 更新时间:2023-12-01 14:33:44 25 4
gpt4 key购买 nike

我正在尝试在网络服务中使用 htmlunit。我创建了一个动态 Web 项目并实现了函数 homepage()。然后我使用 Web 服务向导生成 Web 服务器和客户端。当我尝试使用它时,抛出了这个异常。

Exception: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException; nested exception is: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException Message: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException; nested exception is: java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException

我将 htmunit jar 添加到构建路径中。它没有理由无法找到类。服务器类如下:

package test.eko;
import java.io.IOException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class TestEko {

public String homePage(String place, String checkinDate, String checkoutDate){
final WebClient webClient = new WebClient();
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setPrintContentOnFailingStatusCode(false);

String URL = "http://hotels.travelcomparing.com/SearchResults.aspx?languageCode=EN&currencyCode=EUR&destination=place:" + place + "&radius=0km&checkin=" + checkinDate +"&checkout=" + checkoutDate + "&Rooms=1&adults_1=1&pageSize=15&pageIndex=0&sort=MinRate-asc&showSoldOut=false&view=hc_sr_summary&scroll=0&mapstate=contracted";

HtmlPage page = null;
try {
page = webClient.getPage(URL);
} catch (FailingHttpStatusCodeException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
webClient.waitForBackgroundJavaScript(10000);
String htmlContent = page.asXml();

String hotelName = "";
String priceFrom = "";

String result = "";

for (int i = 0; i < 5; i++){

int index1 = htmlContent.indexOf ( "<div fn=");
int index2 = htmlContent.indexOf("<div fn=", index1 + 8);
String row = (String) htmlContent.subSequence(index1 , index2);
htmlContent = htmlContent.substring(index2);
//System.out.println(row);

int index3 = row.indexOf("class=\"hc_i_hotel\" p=\"2\">");
int index4 = row.indexOf("</a>", index3);
hotelName = (String) row.subSequence(index3 + 61, index4 -34);
System.out.println(hotelName);
result = result + hotelName + " ";

int index5 = row.indexOf("<dd class=\"hc_i_price\">");
int index6 = row.indexOf("</dd>", index5);
String priceRow = (String) row.subSequence(index5, index6 + 5);

int index7 = priceRow.indexOf("<span class=\"hc_pr_syb\">");
int index8 = priceRow.indexOf("<span class=\"hc_pr_cur\">");
priceFrom = (String) priceRow.subSequence(index7 + 148, index8 - 38);
System.out.println(priceFrom);
result = result + priceFrom + " ";
}
return result;


}

}

此代码应采用 3 个字符串作为参数,并使用这些参数访问网站,然后将页面保存到字符串中并解析它以获取一些信息。然后它应该返回连接在单个字符串中的那些信息。该代码在普通项目(不是动态 Web 项目)中正常工作。我对网络服务很陌生,我不确定我做错了什么。有人可以指出我正确的方向吗?

最佳答案

I added the htmunit jars into the buildpath (...) The code worked normally in a normal project (not a dynamic web project)

在处理动态 Web 项目时,所有第三方库都必须位于 WEB-INF/lib 文件夹中,以便 Web 应用程序服务器能够识别和加载。确保执行此操作,然后重新构建您的应用程序并尝试部署它、运行测试等。

关于java - 在 Web 服务中使用 htmlunit 时抛出的异常是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16659478/

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