首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JAVA兑现网址缩短

2012-11-08 
JAVA实现网址缩短腾讯微博发出去的URL网址能够自动缩短。JAVA能否实现网址缩短?答案是可以的。package com.d

JAVA实现网址缩短

腾讯微博发出去的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 + "\"}");               osw.flush();                   BufferedReader br = new BufferedReader(new InputStreamReader(                       conn.getInputStream()));               String line;               while ((line = br.readLine()) != null)               {                   if (line.indexOf("id") > -1)                   {                       shortUrl = line.substring(8, line.length() - 2);                       break;                   }               }               osw.close();               br.close();           } catch (Exception ex)           {               ex.printStackTrace();           }           return shortUrl;       }           public static void main(String[] args)       {           String url = Googl.shorten("http://http://blog.csdn.net/powmxypow");           System.out.println(url);       }   } 


 

热点排行