java,框架springmvc框架,调用的类Unirest
private String getShortUrl(String oriUrl) throws UnirestException {
//oriUrl是老的长连接
//service是新浪短链接的请求地址service=http://api.t.sina.com.cn/short_url/shorten.json
DataSet service = dataSetService.getDataSetByTypeAndCode(ReceptionConst.URL_STRING, ReceptionConst.URL_CODE_05);
//对应得key也是新浪短链接的key一种规范key=3271760578
DataSet source = dataSetService.getDataSetByTypeAndCode(ReceptionConst.APP_KEY, ReceptionConst.APP_KEY_CODE_01);//
String url = service.getDescEn();
String body = Unirest.get(url).queryString("source", source.getDescEn())
.queryString("url_long", oriUrl)
.header("Content-Type", "application/json;charset=UTF-8").asString().getBody();
log.info("sina response: " + body);
Gson g = new Gson();
Object[] map = g.fromJson(body, Object[].class);
Map<String, String> rs = (Map<String, String>) map[0];
return rs.get("url_short");
}
下面是新浪短链接的地址和规范
https://www.douban.com/note/249723561/