9 มกราคม 2009Groovy HTTP Class

/**
 * A Simple HTTP POST/GET Helper Class for 
 *
 * @author 		Tony Landis
 * @copyright 	2007 Tony Landis
 * @website 	http://www.tonylandis.com
 * @license 	BSD License (http://www.opensource.org/licenses/bsd-license.)
 * @example 	h = new GroovyHTTP('http://www.google.com/search')
 *				h.setMethod('GET')
 *				h.setParam('q', '')
 *				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.

Related Blogs


Tags: ,
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply

Comment ไม่ได้กดที่นี่

Get Adobe Flash playerPlugin by wpburn.com wordpress themes