Skip to content

Instantly share code, notes, and snippets.

@aeg
Created May 4, 2013 16:35
Show Gist options
  • Save aeg/5518013 to your computer and use it in GitHub Desktop.
Save aeg/5518013 to your computer and use it in GitHub Desktop.
短縮URLをGroovyで展開する
// Case #1
// 短縮URLを展開する
// 短縮URLが存在しない場合には、groovyx.net.http.HttpResponseException をthrowする
import groovyx.net.http.HttpURLClient
assert 'https://gyakubiki-groovy.rhcloud.com/' == expandURL("http://bit.ly/gyakubikigroovy")
public String expandURL(String urlStr) {
HttpURLClient http = new HttpURLClient(followRedirects:false)
def params = [ url:urlStr,
headers:['User-Agent':'Mozilla/5.0'] ]
def resp = http.request( params )
return resp.headers.'Location'
}
// Case #2
// 短縮URLを展開する。queryStringに文字列を指定するパターンの短縮URLの場合
assert 'http://www.manning.com/hatcher3/' == expandQueryURL("http://r20.rs6.net/tn.jsp",
'001vfwRz2TQN0esdNwdvwqTqzA9FzZPjrdK5Q_rLohllrwrrTbVnJTHQ7GJwnVVBQEWMvgTTePo6ko0i07KsfA_veJitJZU4_Zx8_svrfXSZ0WC6vQoy-PvWAWaIxkVMI5y')
public String expandQueryURL(String urlStr, String queryStr) {
HttpURLClient http = new HttpURLClient(followRedirects:false)
def params = [ url:urlStr,
query:[e:queryStr],
headers:['User-Agent':'Mozilla/5.0'] ]
def resp = http.request( params )
return resp.headers.'Location'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment