Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 6020 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

h = new GroovyHTTP('http://zip4.usps.com/zip4/zcl_0_results.jsp')		 

h.setMethod('POST')

h.setParam('address2', _addr2)

h.setParam('address1', _addr1)

h.setParam('city', _city)

h.setParam('state', _state)

h.setParam('visited', '1')

h.setParam('pagenumber', '0')

h.setParam('firmname', '')

h.setParam('urbanization', '')

h.setParam('zip5', '')

h.open()

h.write()

h.read() 

def _html = h.getContent() 

h.close()



//_html = _html.replaceAll(’(rn?)|n’, ‘ ‘)

//match = _html =~ /



match = []

_html.eachMatch( // ) {match << it[0]}



return match

//return _html



//

// * 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() 

		return writedata

	}



	// 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© 	 

		} 

	} 



	// 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()

	}

}

Above is code that successfully grabs a result page from the USPS Zip+4 website. I am setting all the variable in a script prior executing.

My prob is that I haven't been able to put together a RegExp that will rip the address' and zipcodes from the html result.

I think that part of the problem is r and/or n. I have been unable to remove those from the html code.

When I return _html, the code is what it would be if I had filled in the form myself. So, that is good.

Posted

It's working now. You provide an address with city and state, and it will return the results from the USPS website.

If you are copying and pasting. be sure to remove the space between the & and the nbsp; on 2 of the lines!

I currently have an FM script that passes the contents of the parameters prior to executing the SM script. The error handling is handled by the FM script.

There is no reason why this same Class cannot be used for virtually any HTTPGet/Post operations.

Thanks to jbarnum for the most awesome ScriptMaster plugin and Tony Landis for his HTTP POST/GET Helper Class!

I had to edit the helper to get it to work with SM. Specifically, I had to comment out the '.trim' in the getHeader portion of the class. If anyone fixes that, please post the fix!

h = new GroovyHTTP('http://zip4.usps.com/zip4/zcl_0_results.jsp')		 

h.setMethod('POST')

h.setParam('address2', _addr2)

h.setParam('address1', _addr1)

h.setParam('city', _city)

h.setParam('state', _state)

h.setParam('visited', '1')

h.setParam('pagenumber', '0')

h.setParam('firmname', '')

h.setParam('urbanization', '')

h.setParam('zip5', '')

h.open()

h.write()

h.read() 

def _html = h.getContent() 

h.close()



// get rid of r,  n and t

_html = _html.replaceAll("(rn|r|n|nr|t)", "");

// replace each & nbsp; and 

 with a space

_html = _html.replaceAll("(& nbsp;)", " ");

_html = _html.replaceAll("(

)", " ");



_output = ""

//grab the addresses w/ zips

match = _html =~  //

if(match.count == 0) {return "None found."}

for(i in 0..match.count-1)

{

	_output += match[i][1]

	if(i % 2 == 0) 

		{_output += " "}

	else

		{_output += "n"}

}



return _output



//

// * 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() 

		return writedata

	}



	// 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© 	 

		} 

	} 



	// 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()

	}

}

  • 2 weeks later...
Posted

Hey Jamie - I skimmed over the code, and am wondering if maybe you're working too hard? This code is sending raw HTTP stuff over a socket, but you can do the same thing in very few lines of code using the URLConnection class, which does all the HTTP processing for you.

Posted

In truth, I didn't really have to work that hard, as the Class was written by someone else. I only had to modify 1 line to get it to work with ScriptMaster. I will definitely have a look at the URLconnection Class you mentioned.

Thanks for the feedback.

Posted

Hey Jamie - here's the same functionality written with URLConnection:


URLConnection conn = new URL("http://zip4.usps.com/zip4/zcl_0_results.jsp" ).openConnection();



//Send POST data

conn.setDoOutput( true );

Writer w = new OutputStreamWriter( new BufferedOutputStream( conn.getOutputStream() ) );

w.write( "address2=" + URLEncoder.encode( _addr2 ) );

w.write( "&address1=" + URLEncoder.encode( _addr1 ) );

w.write( "&city=" + URLEncoder.encode( _city ) );

w.write( "&state=" + URLEncoder.encode( _state ) );

w.write( "&visited=1&pagenumber=0&firmname=&urbanization=&zip5=" );

w.close();



//Read result

String _html = conn.getInputStream().getText();



// get rid of r,  n and t

_html = _html.replaceAll("(rn|r|n|nr|t)", "");

// replace each & nbsp; and 

 with a space

_html = _html.replaceAll("(& nbsp;)", " ");

_html = _html.replaceAll("(

)", " ");



_output = ""

//grab the addresses w/ zips

match = _html =~  //

if(match.count == 0) {return "None found."}

for(i in 0..match.count-1)

{

	_output += match[i][1]

	if(i % 2 == 0) 

		{_output += " "}

	else

		{_output += "n"}

}



return _output

Posted

That is awesome! I appreciate the further feedback. Groovy seems to be a great scripting language. But, I have no experience with Java. So, I am experiencing growing pains.

Your example is short, sweet and seems to perform better than the Class I picked up. I will be using it soon. And, of course, I will be implementing it in any future http post/get solutions as well.

Thanks again!

This topic is 6020 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.