/** * A Simple HTTP POST/GET Helper Class for Groovy * * @author Tony Landis * @copyright 2007 Tony Landis * @website http://www.tonylandis.com * @license BSD License (http://www.opensource.org/licenses/bsd-license.php) * @example h = new GroovyHTTP('http://www.google.com/search') * h.setMethod('GET') * h.setParam('q', 'groovy') * h.open() * h.write() * h.read() * println h.getHeader('Server') * println h.getContent() * h.close() */ class GroovyHTTP { public method='POST' public uri public host public path public port public params=null public socket=null public writer=null public reader=null public writedata public headers = [] public content // set the url and create new URI object def GroovyHTTP(url) { uri = new URI(url) host = uri.getHost() path = uri.getRawPath() port = uri.getPort() def tpar = uri.getQuery() if(tpar != null && tpar != '') { tpar.tokenize('&').each{ def pp = it.tokenize('='); this.setParam(pp[0],pp[1]); } } if(port == null || port < 0) port = 80 if(path == null || path.length() == 0) path = "/" } // sets the method (GET or POST) def setMethod(setmethod) { method = setmethod } // push params into this request def setParam(var,value) { if(params != null) params += '&' else params='' params += var +'='+URLEncoder.encode(value) } // clear params def clearParams() { params = null } // open a new socket def open() { socket = new Socket(host, port) } // write data to the socket def write() { def contentLen = 0 if(params!=null) contentLen = params.length() def writedata = ''; if(this.method == 'GET') writedata += "GET " + path +'?'+ params + " HTTP/1.0rn" else writedata += "POST " + path + " HTTP/1.0rn" writedata += "Host: " + host + "rn" + "Content-Type: application/x-www-form-urlencodedrn" + "Content-Length: " + contentLen + "rnrn" + params + "rn" "Connection: closernrn" writer = new PrintWriter(socket.getOutputStream(), true) writer.write(writedata) writer.flush() } // read response from the server def read() { reader = new DataInputStream(socket.getInputStream()) def c = null while (null != ((c = reader.readLine()))) { if(c=='') break headers.add(c) } } // get header value by name def getHeader(name) { def pattern = name + ': ' def result headers.each{ if(it ==~ /${pattern}.*/) { result = it.replace(pattern,'').trim() return 2 } } return result } // get the response content def getContent() { def row content = '' while (null != ((row = reader.readLine()))) content += row + "rn" return content = content.trim(); } // close the socket def close() { reader.close() writer.close() socket.close() } }
จาก http://www.tonylandis.com/code-projects/source/GroovyHTTP.groovy
Related Blogs
- Neatless WordPress Theme
- ALJ Essex Issues Public Version Of Initial Determination in …
- CCNP / BSCI Exam Tutorial: Route Summarization And The OSPF Null …
- Cheap Panasonic HDC-TM55K Hi-Def Camcorder with 8GB Flash Memory …
- SK 91860 1/4-inch Drive 60-Piece 6 Point Socket Super Set …
- The Null Theory « the Air Vent
- Nah Right » Goapele feat. Mos Def – Different (Unreleased)
- Uri amoxil | Fishki.msk.su table
- Stream Def Poetry – Season 1 Online « daphne7565572
- Ad me Up before you gogo » Blog Archive » Overcoming Writer's …
- Ad me Up before you gogo » Blog Archive » The Writer's Edge: Cheap …
- Adventure Travel – Forsake the Well-trodden Path! » Boorstin …
- Tone vs. Content : The Uncredible Hallq
- Local Motors Rally Fighter: Off The Beaten Path | The Truth About Cars
- The Simple Dollar » Remaking Your Path of Least Resistance
- A Path To Prosperity | RedState
- ASUS DSEB-DG – Motherboard – SSI EEB 3.61 – Intel 5400 – LGA771 …
- D-Link DSS-5+ 5-Port 10/100 Switch, Desktop
- Def Comedy Jam: All 11 Episodes | Movie Reviews Time
- Content That Stings


