Using HTMLUnit on a webpage generated by a servlet

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:57:32

问题


How could I use HTMLUnit to get data from a webpage generated by a java servlet. I keep getting an error when I try to read the webpage. /getSurvey is the servlet that creates the webpage but how can I access the HTML generated from the servlet.

final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://survey-creator.appspot.com/getSurvey");


回答1:


HtmlUnit is not really "just" a HTML parser. It's kind of a programmatic webbrowser. It's intented to surf through web pages and/or fill out web forms programmatically using Java language. If your sole purpose is to get the HTML as a String, use a real HTML parser. I can recommend Jsoup for this.

String html = Jsoup.connect("http://stackoverflow.com").get().html();

That's it. It can however do much more than that, such as selecting elements of interest.

See also:

  • Pros and cons of leading HTML parsers in Java


来源:https://stackoverflow.com/questions/5436436/using-htmlunit-on-a-webpage-generated-by-a-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!