groovy使用HTTPBuilder发送HTTP请求
来源:互联网 发布:python为什么叫爬虫 编辑:程序博客网 时间:2024/06/06 03:34
前阵子有一个需求,持续集成平台需要获取一个文件列表,并以可选择参数的方式列出来。列表文件来自于一个web页面,下面挂在了N个可选的文件(build后生成的zip包)
考虑用groovy发送http请求获取response,解析后以列表方式展示,因为groovy是新手,所以走了很多弯路。
1. 首先groovy请求http需要依赖HTTPBuilder模块儿,不知道怎么安装模块儿,辗转了半天,后来发现支持Grab语法,代码如下
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )import groovyx.net.http.HTTPBuilderimport groovyx.net.http.RESTClientimport groovyx.net.http.HttpResponseDecoratorimport static groovyx.net.http.ContentType.*import static groovyx.net.http.Method.*def http = new HTTPBuilder()http.request( 'http://10.1.90.42', GET, TEXT ) { req -> uri.path = '/android_test/releases/test' headers.'User-Agent' = "Mozilla/5.0 Firefox/3.0.4" headers.Accept = 'application/json' response.success = { resp, reader -> assert resp.statusLine.statusCode == 200 println "Got response: ${resp.statusLine}" println "Content-Type: ${resp.headers.'Content-Type'}" println reader.text } response.'404' = { println 'Not found' }}
获取数据后,再进行解析展示,done!
2. 也可以用另一个http请求方式,不用安装module
def connection = new URL("http://10.1.30.42/android_test/releases/test").openConnection()
connection.setRequestMethod('GET')
connection.doOutput = true
def writer = new OutputStreamWriter(connection.outputStream)
writer.flush()
writer.close()
connection.connect()
def respText = connection.content.text
0 0
- groovy使用HTTPBuilder发送HTTP请求
- SOAPUI 使用groovy发送http请求 自动化测试
- Groovy 操作http请求
- 使用GWT发送HTTP请求
- 使用telnet发送HTTP请求
- 使用telnet发送HTTP请求
- 使用SOCKET发送HTTP请求
- 使用Telnet发送HTTP请求
- 使用telnet发送HTTP请求
- 使用HttpURLConnection发送HTTP请求
- 使用telnet发送HTTP请求
- 使用Socket发送Http请求
- 使用Socket发送Http请求
- 使用NSURLSession发送HTTP请求
- 使用java发送HTTP请求
- 使用Oracle发送http请求
- 使用httpurlconnection发送http请求
- 使用scoket发送HTTP请求
- Android--内存管理机制
- iOS 每日一记之 -----点击验证码按钮 获取验证码
- Android AlertDialog.Builder详解
- 第7周 项目2 - 建立链队算法库
- 08-Windows Server 2012 R2 会话远程桌面-标准部署-使用PowerShell进行部署2-1
- groovy使用HTTPBuilder发送HTTP请求
- UVA10635 Prince and Princess 动态规划
- java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
- __block在arc和非arc下含义是否一样
- 09-Windows Server 2012 R2 会话远程桌面-标准部署-使用PowerShell进行部署2-2
- 1 根文件系统 简单来说,(根文件系统)
- “反鸡汤”段子
- 10-Windows Server 2012 R2 会话远程桌面-What's New(RemoteApp)
- superoj925 hello