腾讯微博发出去的URL网址能够自动缩短。JAVA能否实现网址缩短?答案是可以的。
package com.dorole.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class Googl
{
private static String googUrl = "https://www.googleapis.com/urlshortener/v1/url";
public static String shorten(String longUrl)
{
String shortUrl = "";
try
{
URLConnection conn = new URL(googUrl).openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter osw = new OutputStreamWriter(
conn.getOutputStream());
osw.write("{\"longUrl\":\"" + longUrl + "\"}")