package com.yfr.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.security.provider.MD5;
import java.net.URLEncoder;
import java.util.HashMap;
/**
* @Author huanghongfei
* @Date 2018/8/16
* @Descirption 新浪短地址
**/
public class ShortUrlUtils {
private static final Logger log=LoggerFactory.getLogger(ShortUrlUtils.class);
/**
* appkey
*/
private static final String appKey="f1d36e25ab5ea02749f540074214e99d";
/**
* 获取短地址
* @return
*/
public static String getShortUrl(String url){
ShortUrlResponse response=null;
try {
String sign=Md5Utils.encrypt(appKey+Md5Utils.encrypt(URLEncoder.encode(url))).toLowerCase();
String json=HttpUtils.getInstance().httpGet("http://www.mynb8.com/api/sina?appkey="+appKey+"&sign="+sign+"&long_url="+url,new HashMap<>(0));
response=JsonUtils.fromJson(json,ShortUrlResponse.class);
} catch (Exception e) {
e.printStackTrace();
}
if(response.getRs_code()==0){
return response.getData().short_url;
}else{
log.error(response.getRs_msg());
return "";
}
}
public static void main(String[] args) {
System.err.println(ShortUrlUtils.getShortUrl("http://baidu.com"));
}
/**
* 接口响应模型
*/
public static class ShortUrlResponse{
private int rs_code;
private String rs_msg;
private Data data;
public static class Data{
private String appkey;
private String long_url;
private String short_url;
public String getAppkey() {
return appkey;
}
public void setAppkey(String appkey) {
this.appkey = appkey;
}
public String getLong_url() {
return long_url;
}
public void setLong_url(String long_url) {
this.long_url = long_url;
}
public String getShort_url() {
return short_url;
}
public void setShort_url(String short_url) {
this.short_url = short_url;
}
}
public int getRs_code() {
return rs_code;
}
public void setRs_code(int rs_code) {
this.rs_code = rs_code;
}
public String getRs_msg() {
return rs_msg;
}
public void setRs_msg(String rs_msg) {
this.rs_msg = rs_msg;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
}
新浪短地址生成
最新推荐文章于 2024-07-12 15:17:04 发布