长连接地址转为短链接

2018-09-05  本文已影响0人  风中吃西瓜
import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/***
 * 短链接转换工具类
 * 
 * @author Administrator
 *
 */
public class ShortUrlHelper {

    public static CloseableHttpClient httpClient;

    static {
        httpClient = HttpClients.createDefault();
    }


    /**
     * 将长链接转为短链接(调用的新浪的短网址API)
     * 
     * @param url
     *            需要转换的长链接url
     * @return 返回转换后的短链接
     */
    public static String convertSinaShortUrl(String url) {
        try {
            // 调用新浪API
            HttpPost post = new HttpPost("http://api.t.sina.com.cn/short_url/shorten.json");
            List<NameValuePair> params = new LinkedList<NameValuePair>();
            // 必要的url长链接参数
            params.add(new BasicNameValuePair("url_long", url));
            // 必要的新浪key
            params.add(new BasicNameValuePair("source", "3271760578"));
            post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(post);
            // 得到调用新浪API后成功返回的json字符串
            // url_short : 短链接地址 type:类型 url_long:原始长链接地址
            String json = EntityUtils.toString(response.getEntity(), "utf-8");
            JSONArray jsonArray = JSONArray.parseArray(json);
            JSONObject object = (JSONObject) jsonArray.get(0);
            return object.getString("url_short");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 将长链接转为短链接(调用的百度短网址API)
     * 
     * @param url
     *            需要转换的长链接url
     * @return 返回转换后的短链接
     */
    public static String convertBaiDuShortUrl(String url) {
        try {
            // 调用百度API
            HttpPost post = new HttpPost("http://www.dwz.cn/create.php");
            List<NameValuePair> params = new LinkedList<NameValuePair>();
            // 必要的url长链接参数
            params.add(new BasicNameValuePair("url", url));
            post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(post);
            // 得到调用百度API后成功返回的json字符串
            // tinyurl : 短链接地址 status:0 表示转换成功 非0表示转换失败 longurl:原始长链接地址 err_msg:错误信息
            String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
            JSONObject object = JSON.parseObject(jsonStr);
            return object.getString("tinyurl");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }
    
    /**
     * 测试
     * @param args
     */
     public static void main(String []args){
         String tinyurl = convertBaiDuShortUrl("http://news.sina.com.cn/gov/xlxw/2018-09-05/doc-ihiixyeu3395739.shtml");
         System.out.println(tinyurl);
     }
}

上一篇下一篇

猜你喜欢

热点阅读