html - 从groovy中的响应中获取html正文

标签 html groovy

我试图查看 html 页面中是否存在特定字符串,但我似乎找不到一种简单的方法来获取代表正文的字符串。

我尝试过:

http.request(Method.GET, { req ->
        uri.path = '/x/app/main'
        response.success = { resp, reader ->
            assert resp.status == 200
            println reader.text.startsWith('denied')
        }

        response.failure = { resp ->
            fail("Failure reported: ${resp.statusLine}")
        }
    })

但是 reader.text 是一个 NodeChildren 对象。

如何获取字符串形式的 html(或更具体地说,正文的上下文)?

最佳答案

您可以直接从响应中获取输入流。试试这个:

http.request(Method.GET, { req ->
    uri.path = '/x/app/main'
    response.success = { resp ->
        assert resp.status == 200
        println resp.entity.content.text.startsWith('denied')
    }

    response.failure = { resp ->
        fail("Failure reported: ${resp.statusLine}")
    }
})

关于html - 从groovy中的响应中获取html正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8729159/

相关文章:

Android - 错误 :A prblem occurred configuring project ':app'

css - 背景图片上的html div

html - 在我自己的网站上复制 Google 的 "I' m Feeling Lucky"按钮——自动将用户带到顶部搜索结果

jquery - 我有一个多级下拉菜单,当我们从菜单中鼠标移出时,我想给一些时间

mysql - Grails GORM 如何在我的应用程序中加载现有的远程数据库表

groovy - GEB:禁用 waitFor block 中的隐式断言

javascript - 将iframe内html文件的元素添加到原始文件的DOM树中

html - CSS3隐藏div在悬停时淡入淡出?

groovy - Groovy 中的安全范围运算符?

Tomcat 记录 http 请求并调用 java 代码