【问题标题】:HTML form handling in JavaJava中的HTML表单处理
【发布时间】:2014-05-02 14:59:04
【问题描述】:

我正在开发一个使用以下网站的 Java 项目: http://feedenlarger.com/

程序本质上只是在“输入部分提要 URL”框中输入一个链接 [ 例如 http://feeds.bbci.co.uk/news/rss.xml ] 并提交表单。提交表单后,我想下载用户手动填写表单并单击页面上的按钮时将访问的页面。

如何在 Java 中做到这一点?

我已成功使用以下表单下载页面:

    private String readWebPage() throws IOException{
        URL u = null;
        URLConnection urlC = null;
        BufferedInputStream bis = null;
        try{
            u = new URL(this.url);
            urlC = u.openConnection();
            headers = urlC.getHeaderFields();

            bis = new BufferedInputStream(urlC.getInputStream());

            StringBuilder builder = new StringBuilder();
            int byteRead;
            while ((byteRead = bis.read()) != -1)
                builder.append((char) byteRead);

            bis.close();
            return builder.toString();
        } catch(IOException e){
            System.out.println("Webpage: IO Error");
            throw e;
        }
    }

注意:我有兴趣将标头保存在内存中,因此最好使用 URLConnection 或类似方法。

我现在如何填写表格、使用 POST/GET 请求提交并下载下一个网页?提交表单后,我尝试从浏览器生成的link 下载页面,但这给了我一个“禁止”错误。

我一直在使用的可编译项目可用here

【问题讨论】:

  • 你这样做是为了测试还是因为你想在后台模拟用户?
  • 测试目的。我正在尝试学习如何使用 Java 填写表格。这是我能找到的最简单的网站/表格。欢迎使用不同网站的示例:)
  • 如果是为了测试您的网站 onyl,您应该查看Selenium 而不是重新发明轮子。如果您确实需要在后台执行 GET/POST 请求(这非常罕见但可能会发生),那么您应该使用另一种技术,如 jsoup,它允许读取整个 HTML 并继续使用 URLConnection 或 Apache HttpComponents 来执行对服务器的请求。
  • 我只需要能够从提交表单时打开的第二个网页中获取信息。我有使用 JSOUP 解析网页的经验,虽然我只需要能够下载它,但我不需要解析第二个网页。
  • 有没有办法可以在不加载第一页的情况下下载第二页?那将是最理想的解决方案。

标签: java forms post urlconnection


【解决方案1】:

(将其作为答案发布,因为它太大而无法放入评论中。另外,作为社区 wiki 发布以避免获得任何代表 [或在有人没有阅读此消息的情况下失去它])。

这是我在页面上发送最后一条评论时网络监视器的结果:

Remote Address:198.252.206.140:80
Request URL:http://stackoverflow.com/posts/23431154/comments
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:es,en-US;q=0.8,en;q=0.6,pt;q=0.4,fr;q=0.2
Connection:keep-alive
Content-Length:322
Content-Type:application/x-www-form-urlencoded
Cookie:__qca=P0-1914216052-1380726140973; __utma=140029553.1039400677.1380726141.1389622782.1389628108.351; __utmz=140029553.1389047375.333.15.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); sgt=id=dfc633fa-3459-4f24-be6f-ca2ee08908cd; usr=t=f94uBq5WkGsH&s=BDL1eqRYkOQ5&p=[2|2][10|15]; _ga=GA1.2.1039400677.1380726141
Host:stackoverflow.com
Origin:http://stackoverflow.com
Referer:http://stackoverflow.com/questions/23431154/html-form-handling-in-java?noredirect=1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
comment:Check out all the fields you need to sent and which of them are auto generated by the server (usually hidden fields). Basically, you need to check all the elements about how the request is being sent. This can be easily done in Chrome or Firefox + Firebug by pressing F12.
fkey:bc6f108950fe59611b3f1ebf4caedb31
Response Headersview source
Cache-Control:private
Content-Encoding:gzip
Content-Length:2158
Content-Type:text/html; charset=utf-8
Date:Fri, 02 May 2014 15:21:55 GMT
Pragma:no-cache
Vary:Accept-Encoding
X-Frame-Options:SAMEORIGIN

这是网络监视器外观的示例

【讨论】:

    【解决方案2】:

    根据您的场景,我们可以使用 HMTLUnit 和 Selenium 工具来实现。

    您可以使用 HtmlTextInput 输入文本并提交表单以使用锚标记导航到第二页。

    我们完全支持 HTMLUnit 中的 HTML 标签。 使用 HTMLInput 使用 setAttributeValue("some text") 方法在第一页中设置值,然后使用您正在寻找的按钮或锚标记进入下一个页面。

    HtmlAnchor anchor = (HtmlAnchor)page.getHtmlElementById("second_page_link");
        page = (HtmlPage) anchor.click();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      相关资源
      最近更新 更多