1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package org.grails.shortenurl class IsGd{ static String shorten(String longUrl){ def addr = "http://is.gd/api.php?longurl=${longUrl}" def url = addr.toURL() def urlConnection = url.openConnection() if(urlConnection.responseCode == 200){ return urlConnection.content.text }else{ return "An error occurred: ${addr}\n" + "${urlConnection.responseCode} : ${urlConnection.responseMessage}" } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package org.grails.shortenurl class IsGdTests extends GroovyTestCase{ def transactional = false void testShorten(){ def shortUrl = IsGd.shorten("http://grails.org") assertEquals "http://is.gd/2oCZR", shortUrl } void testBadUrl(){ def shortUrl = IsGd.shorten("IAmNotAValidUrl") println shortUrl assertTrue shortUrl.startsWith("An error occurred:") } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $ curl --verbose "http://is.gd/api.php?longurl=IAmNotAValidUrl" * About to connect() to is.gd port 80 (#0) * Trying 78.31.109.147... connected * Connected to is.gd (78.31.109.147) port 80 (#0) > GET /api.php?longurl=IAmNotAValidUrl HTTP/1.1 > User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 > Host: is.gd > Accept: */* > < HTTP/1.1 500 Internal Server Error < X-Powered-By: PHP/5.2.6 < Content-type: text/html; charset=UTF-8 < Transfer-Encoding: chunked < Date: Wed, 19 Aug 2009 17:33:04 GMT < Server: lighttpd/1.4.22 < * Connection #0 to host is.gd left intact * Closing connection #0 Error: The URL entered was not valid. |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |