我们可以使用HttpBuilder请求https链接吗?
1 - 如果是,那么它们是我们在请求数据时需要设置的任何特定参数或属性。
2 - 如果没有,那么我们可以请求与普通的http链接相同,如下所示。
def http = new HTTPBuilder('http://some_link');
def resp = http.get(path: '/abcd/efg', query:[login:'login_id', pass: 'password',ttype:'trans_type',prodid:'prod_id'], contentType : XML, headers : [Accept : 'application/xml'] )
答案 0 :(得分:1)
刚尝试过,它确实像文档中所说的那样工作......
@Grab( 'org.ccil.cowan.tagsoup:tagsoup:1.2' )
@Grab( 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2' )
import org.ccil.cowan.tagsoup.Parser
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
// Using an https url
new HTTPBuilder( 'https://twitter.com' ).with {
// Get from the given path as TEXT
get( path:'/tim_yates', contentType:TEXT ) { resp, reader ->
// Pass the html through tagsoup and generate a parser
new XmlParser( new Parser() ).parseText( reader.text ).with {
// print the title text from inside the head section
println head.title.text()
}
}
}