Friday, January 29, 2016

HEAD HTTP Request

boolean isValidUrl(String url) {
 try {
  HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection()
  connection.setRequestMethod('HEAD')
  if (connection.getResponseCode() == 200) {
   return true
  }
 }
 catch(final MalformedURLException e) { }
 catch(final IOException e) { }

 return false
}

Tuesday, January 5, 2016

GZip HTTP Request/Response

import java.util.zip.GZIPInputStream

String pageUrl = "http://www.google.com"
BufferedInputStream buff = pageUrl.toURL().newInputStream(requestProperties: ['Accept-Encoding': 'gzip,deflate'])
BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(buff)))
String resp = ""; reader.eachLine{ line -> resp += line }