我可以在Web服务中使用htmlunit吗?

时间:2013-05-20 03:27:28

标签: java html web-services webserver htmlunit

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

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

  1. 创建动态网络项目
  2. 将htmlunit jar添加到构建路径中。
  3. 将工作junit测试作为一个函数实现并添加到项目中
  4. 使用eclipse的Web服务向导创建Web服务器和客户端。
  5. 之后,向导生成了一个带有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.
    

1 个答案:

答案 0 :(得分:0)

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