Java
bit.ly api java 구현
seongsland
2016. 10. 6. 15:58
bit.ly 를 사용하려면 먼저 회원가입을 하고 login id랑 apikey를 발급 받아야 함.
url 생성은 한달에 5,000개까지 가능함.
String url = "http://api.bit.ly/v3/shorten";
String changeUrl = "http://seongsland.tistory.com";
String param = "login=[YOURID]&apiKey=[YOURKEY]&format=txt&uri=" + changeUrl;
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream os = con.getOutputStream();
os.write(param.getBytes());
os.flush();
os.close();
if(con.getResponseCode() < 400) {
InputStream is = con.getInputStream();
byte[] read = new byte[1024];
while(is.read(read, 0, 1024) > -1) {
System.out.println(new String(read));
}
is.close();
}
con.disconnect();