기본정보#

2009년 1월 현재 최신버전은 0.30입니다.

HTTPBuilder가 Apache의 HttpClient의 래퍼이기 때문에 내부적으로 필요한 jar파일이 있습니다. target/dependencies 디렉터리밑에 모두 있습니다.

  • 기능
    • XML, JSON, HTML을 지원하는 Builder 와 파서
    • GZIP과 컨텐츠 인코딩 지원
    • HTTP Basic, Digest 와 SSL 인증 지원
    • 상태 코드에 기초한 응답 핸들링
    • GET, POST 를 위한 편리한 메서드
    • 비동기 요청을 위한 AsyncHTTPBuilder

샘플 #

import groovyx.net.http.*;
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder('http://www.google.com')

http.get( path : '/search',
          contentType : TEXT,
          query : [q:'Groovy'] ) { resp, reader ->

  println "response status: ${resp.statusLine}"
  println 'Response data: -----'
  System.out << reader
  println '\n--------------------'
}
import groovyx.net.http.*;
import static groovyx.net.http.Method.GET;
import static groovyx.net.http.ContentType.JSON;

def http = new HTTPBuilder( 'http://ajax.googleapis.com' )

// perform a GET request, expecting JSON response data
http.request( GET, JSON ) {
  url.path = '/ajax/services/search/web'
  url.query = [ v:'1.0', q: 'Calvin and Hobbes' ]

  headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'

  // response handler for a success response code:
  response.success = { resp, json ->
    println resp.statusLine

    // parse the JSON response object:
    json.responseData.results.each {
      println "  ${it.titleNoFormatting} : ${it.visibleUrl}"
    }
  }

  // handler for any failure status code:
  response.failure = { resp ->
    println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
  }
}
import groovyx.net.http.HTTPBuilder;
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

def http = new HTTPBuilder('http://www.google.com')

http.request(GET,TEXT) { req ->
  url.host = 'www.google.com' // overrides default URL
  headers.'User-Agent' = 'Mozilla/5.0'

  response.success = { resp, reader ->
    println 'my response handler!'
    assert resp.statusLine.statusCode == 200
    println resp.statusLine
    System.out << reader // print response stream
  }

  response.'404' = { resp ->  // fired only for a 401 (access denied) status code
    println 'Not found'
  }
}

Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
png
1.png 2.7 kB 1 11-Jan-2009 14:56 DongGukLee
png
2.png 5.3 kB 1 11-Jan-2009 14:56 DongGukLee
« This page (revision-4) was last changed on 11-Jan-2009 15:48 by DongGukLee