package com.magicstudio.hiver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import org.json.simple.parser.ContainerFactory;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ShortUtil {
private static Map parseJson2Map(String strResponseJson){
JSONParser parser = new JSONParser();
ContainerFactory containerFactory = new ContainerFactory(){
public List creatArrayContainer() {
return new LinkedList();
}
public Map createObjectContainer() {
return new LinkedHashMap();
}
};
try{
return (Map)parser.parse(strResponseJson, containerFactory);
}
catch(ParseException pe){
System.out.println(pe);
}
return null;
}
private static String getJsonValue(Map json, String key){
if (json == null || key == null || key.equals("")){
return "";
}
else{
return json.get(key).toString();
}
}
public static String doGet(String strGetURL) {
String response = "";
HttpURLConnection connection = null;
try {
URL getUrl = new URL(strGetURL);
if (strGetURL.toLowerCase().startsWith("https://")){
connection = (HttpsURLConnection) getUrl
.openConnection();
}
else{
connection = (HttpURLConnection) getUrl
.openConnection();
}
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()
// 函数中才会真正发到服务器
connection.connect();
int code = connection.getResponseCode();
System.out.println("code " + code);
InputStream is = connection.getInputStream();
response = convertStreamToString(is);
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (connection != null)
connection.disconnect();
}
return response;
}
public static String doPost(String strPostURL, String strParameters) {
String response = "";
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL(strPostURL);
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoInput(true);
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.getOutputStream().write(strParameters.getBytes("utf-8"));
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
int code = httpurlconnection.getResponseCode();
System.out.println("code " + code);
InputStream is = httpurlconnection.getInputStream();
response = convertStreamToString(is);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
return response;
}
public static String convertStreamToString(InputStream is)
throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
return sb.toString();
}
public static String getBaiduShortURL(String strLongURL){
String strPostURL = "http://dwz.cn/create.php";
//code 200
//{"longurl":"http:\/\/www.efeihu.com\/Product\/2010101036233.html?pcid=hp5-3","status":0,"tinyurl":"http:\/\/dwz.cn\/6Nars"}
//http://dwz.cn/6Nars
String strResponse = doPost(strPostURL, "url=" + strLongURL);
//String strResponse = "{\"longurl\":\"http:\\/\\/www.efeihu.com\\/Product\\/2010101036233.html?pcid=hp5-3\",\"status\":0,\"tinyurl\":\"http:\\/\\/dwz.cn\\/6Nars\"}";
if (strResponse == null || strResponse.equals("")){
return strLongURL;
}
else{
return getJsonValue(parseJson2Map(strResponse),"tinyurl");
}
}
public static String getNeteaseShortURL(String strLongURL){
String strPostURL = "http://126.am/api!shorten.action";
String strParameters = "key=c1b32ba732134a53b0bb287d55841a91&longUrl=" + strLongURL;
//code 200
//{"status_code":200,"longUrl":"http:\/\/www.efeihu.com\/Product\/2010101036233.html?pcid=hp5-3","status_txt":"OK","url":"126.am\/cs1yr4"}
String strResponse = doPost(strPostURL, strParameters);
//String strResponse = "{\"status_code\":200,\"longUrl\":\"http:\\/\\/www.efeihu.com\\/Product\\/2010101036233.html?pcid=hp5-3\",\"status_txt\":\"OK\",\"url\":\"126.am\\/cs1yr4\"}";
if (strResponse == null || strResponse.equals("")){
return strLongURL;
}
else{
return getJsonValue(parseJson2Map(strResponse),"url");
}
}
public static String getGoogleShortURL(String strLongURL) {
//http://goo.gl/8qEyO
return Google.shorten(strLongURL);
}
public static String getTinyTWShortURL(String strLongURL) {
String strURL = "http://tiny.tw/api.php?format=text&url=" + strLongURL;
//code 200
//API currently being upgraded
return doGet(strURL);
}
public static String getSinaShortURL(String strLongURL){
String strGetURL = "https://api.weibo.com/2/short_url/shorten.json?source=2860909371&url_long="+ strLongURL;
//太麻烦了,放弃
return doGet(strGetURL);
}
public static String getQQShortURL(String strLongURL){
String appKey = "801332473";
String strURL = "http://open.t.qq.com/api/short_url/shorten?format=xml&long_url=" + strLongURL +
"&appid=xx&openid=xx&openkey=xx&clientip=xx&reqtime=xx&sig=xx&wbversion=1";
//App Key:801332473
//App Secret:97ab69eedbf8455b095ec8242fb5a6a4
//太麻烦了,放弃
return "";
}
public static String getShortURL(String strLongURL){
String strPostURL = "http://www.shorturl.com/make_shorturl.php";
String strParameters = "longurl=" + strLongURL;
// <html><head><title>ShortURL.com - free short URL redirection with no ads!</title><META NAME="description" CONTENT="Free URL redirection service (also known as URL forwarding). Register a free subdomain name and redirect it to your existing URL! No ads, loaded with url redirect features."><META NAME="keywords" CONTENT="short url, free url redirection, url forwarding, url redirect, url forward, url redirection, forward url, redirect url, free url, free url forwarding, free sub domain, subdomain, subdomains, URL redrection, subdomain names, subdomain name, sub domains, redirection, redirect"><meta name="Robots" content="INDEX,FOLLOW"><LINK REL="SHORTCUT ICON" HREF="http://www.ShortURL.com/ShortURL.ico"><BASE HREF="http://www.ShortURL.com"><style type="text/css"><!--a.mainNav:link { /* Applies to unvisited links of class mainNav */ text-decoration: none; font-weight: bold; color: #3366cc; font-size: 13px; font-family: Arial, Helvetica, sans-serif; } a.mainNav:visited { /* Applies to visited links of class mainNav */ text-decoration: none; font-weight: bold; color: #3366cc; font-size: 13px; font-family: Arial, Helvetica, sans-serif; } a.mainNav:hover { /* Applies to links under the pointer of class mainNav */ text-decoration: none; font-weight: bold; color: red; font-size: 13px; font-family: Arial, Helvetica, sans-serif; } a.mainNav:active { /* Applies to activated links of class mainNav */ text-decoration: none; font-weight: bold; color: #0066FF; font-size: 13px; font-family: Arial, Helvetica, sans-serif; } .hi
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JavaURLShorter 20130326.7z (29个子文件)
JavaURLShorter
.project 390B
src
com
magicstudio
hiver
ShortUtil.java 44KB
Google.java 2KB
URLShorter.java 19KB
.settings
org.eclipse.jdt.core.prefs 629B
libs
json-simple-1.1.1.jar 23KB
gson-1.3.jar 153KB
.classpath 422B
bin
com
magicstudio
hiver
ShortUtil.class 7KB
GsonGooGl.class 640B
URLShorter$7.class 752B
URLShorter$14.class 1KB
URLShorter$6.class 748B
URLShorter$8.class 751B
ShortUtil$1.class 786B
URLShorter$2.class 706B
URLShorter$9.class 754B
URLShorter$1.class 736B
URLShorter$12.class 913B
URLShorter$15.class 1017B
GooGlResult.class 918B
URLShorter$5.class 749B
URLShorter$11.class 753B
URLShorter$4.class 757B
URLShorter.class 13KB
Google.class 3KB
URLShorter$10.class 755B
URLShorter$13.class 2KB
URLShorter$3.class 754B
共 29 条
- 1
资源评论
- 追赶的蜗牛2013-08-22虽然没帮我解决问题,还是学习啦,谢谢
- wanghuibo2014-01-22看代码写的很好,我没有对应的框架,没有运行起来。
- gaopz1232015-07-15不错,学习了
dumbbellyang
- 粉丝: 62
- 资源: 106
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功