从groovy的响应中获取html正文

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