2013-05-20 34 views
1

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

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

  1. 创建动态Web项目
  2. 添加化的HtmlUnit罐子的进入构建路径。
  3. 实现工作junit测试作为函数并添加到项目
  4. 使用eclipse的Web服务向导创建Web服务器和客户端。

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

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

因为我是真正的新的Web服务我都做一个可怕的错误,当我想在本教程简单的Web服务的步骤将这项工作或这是一些错误那可以修复?一般来说,我可以用这种方式实际使用htmlunit还是完全不可能?

的Web服务器的代码如下:

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

是的,您可以使用htmlunit,前提是web服务器(通过web服务)运行的机器至少可以访问互联网(或该网站)。你的异常看起来太小了,你可以发布整个堆栈跟踪吗? – acdcjunior

+0

我会将整个堆栈跟踪添加到问题中。顺便问一下,你知道我是否也可以用相同的方式使用硒?我不确定,因为firefox窗口弹出来了。 – ManahManah

+1

你可以使用硒和htmlunit作为浏览器,而不是firefox。正如你所预料的那样,Firefox本身就会打开窗户。有办法不让它打开窗口(比如设置一个selenium远程服务器,或者其他的东西),但它的高级内容。 – acdcjunior

回答

0

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

相关问题