crazy4groovy
9/25/2013 - 2:33 AM

Get the HTTP response code of any URL (with an optimal HEAD request); returns an Integer.

Get the HTTP response code of any URL (with an optimal HEAD request); returns an Integer.

def getResponseCode = { String urlString ->
  URL u = new URL(urlString)
  HttpURLConnection huc = (HttpURLConnection) u.openConnection()
  huc.setRequestMethod("HEAD")
  huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)")
  huc.connect()
  //println huc.getResponseCode()
  return huc.getResponseCode()
}