从groovy的响应中获取html正文 [英] Get html body from response in groovy

查看:673
本文介绍了从groovy的响应中获取html正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to see if a specific string exists in an html page but I can't seem to find an easy way to get the string that represents the body.

我试过:

I've attempted:

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对象。

but reader.text is a NodeChildren object.

我如何获得html(或更具体地说,是body的上下文)作为字符串?

How do I get the html (or more specifically, the contexts of the body) as a string?

推荐答案

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

You can get an input stream directly off of the response. Try this:

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}")
    }
})

这篇关于从groovy的响应中获取html正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆