Here you can find the source of shortenUrl(String url)
private static String shortenUrl(String url)
//package com.java2s; //License from project: Apache License import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class Main { private static String shortenUrl(String url) { String shortUrl = ""; try {//from www . j ava2s .c o m URLConnection conn = new URL("https://www.googleapis.com/urlshortener/v1/url").openConnection(); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/json"); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write("{\"longUrl\":\"" + url + "\"}"); wr.flush(); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { if (line.indexOf("id") > -1) { //hack shortUrl = line.substring(8, line.length() - 2); break; } } wr.close(); rd.close(); } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return shortUrl; } }