1

我一直在尝试在 Web 服务功能中使用 htmlunit。我是 Web 服务的新手,我使用本教程来实现一个简单的 Web 服务: http: //www.eclipse.org/webtools/community/education/web/t320/Implementing_a_Simple_Web_Service.pdf

然后我尝试实现一个 Web 服务,它将获取 3 个字符串作为参数,使用 htmlunit 访问一个网站,用 3 个字符串填写网站中的表单并单击搜索按钮,解析结果页面并返回一个包含结果页面的信息。我采用了幼稚的方法,并尝试使用与教程相同的步骤来实现该 Web 服务。步骤是:

  1. 创建动态 Web 项目
  2. 将 htmlunit jar 添加到构建路径中。
  3. 将工作的junit测试作为一个函数实现并添加到项目中
  4. 使用 Eclipse 的 Web 服务向导创建 Web 服务器和客户端。

之后,向导生成了一个带有 3 个文本框的简单界面,我输入了在 junit 测试项目中起作用的值。但我有一个例外说:

Exception: java.lang.reflect.InvocationTargetException Message: java.lang.reflect.InvocationTargetException

由于我对 Web 服务真的很陌生,当我认为本教程中简单 Web 服务的步骤可以解决这个问题时,我是否犯了一个可怕的错误,或者这是一些可以修复的错误?一般来说,我可以以这种方式实际使用 htmlunit 还是完全不可能?

网络服务器的代码如下:

package webService.test.TravelComparing;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class TravelComparingTest {

public String homePage(String place, String checkinDate, String checkoutDate) throws Exception {
    final WebClient webClient = new WebClient();

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 = webClient.getPage(URL);
webClient.waitForBackgroundJavaScript(10000);
String htmlContent = page.asXml();
Writer out = new BufferedWriter(new OutputStreamWriter(
        new FileOutputStream("travelcomparing.html"), "UTF-8"));
    try {
        out.write(htmlContent);
    } finally {
        out.close();
    }

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

}

编辑:

tomcat 控制台的完整内容如下。我提到的异常显示在客户端的结果视图中,我在其他任何地方都找不到。

May 20, 2013 7:17:15 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\eclipse;;.
May 20, 2013 7:17:15 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TravelComparingTest' did not find a matching property.
May 20, 2013 7:17:15 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TravelComparingTestClient' did not find a matching property.
May 20, 2013 7:17:15 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TravelComparing2' did not find a matching property.
May 20, 2013 7:17:15 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TravelComparing2Client' did not find a matching property.
May 20, 2013 7:17:15 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:traveltest' did not find a matching property.
May 20, 2013 7:17:15 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:traveltestClient' did not find a matching property.
May 20, 2013 7:17:15 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
May 20, 2013 7:17:15 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
May 20, 2013 7:17:15 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 410 ms
May 20, 2013 7:17:15 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 20, 2013 7:17:15 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.40
May 20, 2013 7:17:16 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
May 20, 2013 7:17:17 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
May 20, 2013 7:17:17 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
May 20, 2013 7:17:17 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 20, 2013 7:17:17 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 20, 2013 7:17:17 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2044 ms
May 20, 2013 7:17:19 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
4

1 回答 1

0

我终于通过删除文件编写代码并用 try catch 块包围所有必要字段来解决了这个问题。抛出这个异常的原因似乎是函数中抛出了io异常,但是使用reflect时,它显示的是问题中的异常而不是io异常。

于 2013-05-21T01:10:00.647 回答