java 得到短链接

来源:互联网 发布:突破网络系统的第一步 编辑:程序博客网 时间:2024/05/22 08:29

java,框架springmvc框架,调用的类Unirest

private String getShortUrl(String oriUrl) throws UnirestException {//oriUrl是老的长连接                //service是新浪短链接的请求地址service=http://api.t.sina.com.cn/short_url/shorten.jsonDataSet service = dataSetService.getDataSetByTypeAndCode(ReceptionConst.URL_STRING, ReceptionConst.URL_CODE_05);//对应得key也是新浪短链接的key一种规范key=3271760578DataSet 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/


0 0
原创粉丝点击