【短链接】——新浪、百度、搜狐等官方长链接转短链接

在项目中难免会生成自己的跳转链接,为了简化链接以及防止微信等屏蔽,可以用一些官方的api将自己的链接转为短链接。

ConvertShortUrlUtil.java

public class ConvertShortUrlUtil {

    public static CloseableHttpClient httpClient;

    static {
        httpClient = HttpClients.createDefault();
    }

    /**
     * 将长链接转为短链接(调用的缩短网址API)
     * 需要token
     * @param longUrl 需要转换的长链接url
     * @return 返回转换后的短链接
     */
    public static String longUrl2Short(String longUrl) {
        String result = null;
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            String temp = URLEncoder.encode(longUrl, "UTF-8");
            HttpGet request = new HttpGet("http://api.suolink.cn/api.php?url=" + temp);
            request.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4)");

            HttpResponse response = client.execute(request);
            result = EntityUtils.toString(response.getEntity());
            if (!result.startsWith("http")) {
                result = null;
            }
        } catch (Exception e) {
            result = null;
            Logger.warn(e);
        }
        if (StringUtils.isEmpty(result)) {
            result = longUrl;
        }
        return result;
    }

    /**
     * 将长链接转为短链接(调用的新浪的短网址API)
     * 需要source 
     * @param url
     *        需要转换的长链接url
     * @return 返回转换后的短链接
     */
    public static ResultBean convertSinaShortUrl(String url) {
        ResultBean result = new ResultBean<>();
        try {
            // 调用新浪API
            HttpPost post = new HttpPost("http://api.t.sina.com.cn/short_url/shorten.json");
            List params = new LinkedList();
            // 必要的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");
            JSONObject object;
            try {
                object = JSONObject.parseObject(json);
                if (object != null) {
                    result.setCode(object.getInteger("error_code"));
                    result.setMessage(object.getString("error"));
                }
            } catch (JSONException e) {
                JSONArray jsonArray = JSONArray.parseArray(json);
                object = (JSONObject) jsonArray.get(0);
                if (object != null) {
                    result.setCode(object.getInteger("type"));
                    result.setData(object.getString("url_short"));
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 将长链接转为短链接(调用的百度短网址API)
     * 需要 token
     * @param longUrl
     *            需要转换的长链接url
     * @return 返回转换后的短链接
     */
    public static ResultBean convertBaiDuShortUrl(String longUrl) {
        ResultBean result = new ResultBean<>();
        try {
            String uri = "https://dwz.cn/admin/v2/create";
            HttpPost httpost = new HttpPost(uri);
            httpost.addHeader("Token","dbbe913a3d0b7634a3f24526ce13a967");
            List params = new LinkedList();
            params.add(new BasicNameValuePair("Url", longUrl));
            // 用户名称
            httpost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
            HttpResponse response = httpClient.execute(httpost);
            String json = EntityUtils.toString(response.getEntity(), "utf-8");
            JSONObject object = JSONObject.parseObject(json);
            if (object != null) {
                result.setCode(object.getInteger("Code"));
                result.setData(object.getString("ShortUrl"));
                result.setMessage(object.getString("ErrMsg"));
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 将长链接转为短链接(调用的搜狐短网址API)
     * 需要 token
     * @param longUrl
     *            需要转换的长链接url
     * @return 返回转换后的短链接
     */
    public static ResultBean convertSohuShortUrl(String longUrl) {
        ResultBean result = new ResultBean<>();
        try {
            String uri = "https://sohu.gg/api?format=json";
            HttpPost httpost = new HttpPost(uri);
            httpost.addHeader("key","41ab8c3a");
            List params = new LinkedList();
            params.add(new BasicNameValuePair("url", longUrl));
            // 用户名称
            httpost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
            HttpResponse response = httpClient.execute(httpost);
            String json = EntityUtils.toString(response.getEntity(), "utf-8");
            JSONObject object = JSONObject.parseObject(json);
            if (object != null) {
                result.setCode(object.getInteger("error"));
                result.setData(object.getString("short"));
                result.setMessage(object.getString("msg"));
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

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

ResultBean.java

public class ResultBean {
    
    privare Integer code;
    private String message;
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

     @Override
    public String toString() {
        return "ResultBean{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

 

你可能感兴趣的:(★JAVA学习之路★)