JAVA实现网址缩短
2012-10-30 21:45
197 查看
腾讯微博发出去的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); } }
相关文章推荐
- Google App Engine Web开发之java类到网址的映射——实现用java类处理jsp页面的提交 推荐
- (转)Java实现利用搜索引擎收集网址的程序
- Java实现利用搜索引擎收集网址的程序
- java实现打开浏览器并跳到指定网址的类
- 【转】微博URL短网址生成算法原理及(java版、php版实现实例)
- Java实现利用搜索引擎收集网址的程序
- Java实现利用搜索引擎收集网址的程序
- Java实现利用搜索引擎收集网址的程序
- 怎么实现利用Java搜索引擎收集网址的程序
- 基于java实现rss阅读器,可实现读取不同新闻聚合类网址的内容并且显示
- Java实现利用搜索引擎收集网址的程序
- htaccess 实现网址缩短
- Java实现利用搜索引擎收集网址的程序
- 微博缩短网址是如何实现的
- 如何用Java实现利用搜索引擎收集网址的程序
- 微博URL短网址生成算法原理及(java版、php版实现实例)
- 微博缩短网址的实现
- 微博URL短网址生成算法原理及(java版、php版实现实例)
- Java实现利用搜索引擎收集网址的程序
- Java实现利用搜索引擎收集网址的程序