短网址api对接成了大家经常遇到的难题,今天以 t.cn短链接api接口 为例给出三种语言的调用示例代码:
http://url-t.cn/api
$url = 'http://www.baidu.com'; $api/_url = 'http://url-t.cn/tcn/api?key=APIKEY/tcn/_url?url='.urlencode($url); $short/_url = file/_get/_contents($api/_url); echo $short/_url;
public static void main(String path/[/]) throws Exception { URL u = new URL("http://url-t.cn/tcn/api?key=APIKEY&url=http%3A%2F%2Fwww.baidu.com"); InputStream in = u.openStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { byte buf/[/] = new byte/[1024/]; int read = 0; while ((read = in .read(buf)) > 0) { out.write(buf, 0, read); } } finally { if ( in != null) { in .close(); } } byte b/[/] = out.toByteArray(); System.out.println(new String(b, "utf-8")); }
import urllib, urllib2, sys host = 'http://url-t.cn' path = '/tcn/api' method = 'GET' querys = 'key=APIKEY&url=http%3A%2F%2Fwww.baidu.com' bodys = {} url = host + path + '?' + querys request = urllib2.Request(url) response = urllib2.urlopen(request) content = response.read() if (content): print(content)