今天给大家案例短链接接口,短链接接口是一种用于将长网址转换为短网址的技术接口。
那大家知道短链接接口的应用场景吗?
它具有以下一些主要特点和作用:
1.节省空间:在有限的显示区域,如社交媒体帖子、短信等中,短链接更简洁,不占太多字符。
2.便于传播:简短且易记,更易于用户分享和传播。
例如,在微博中分享一篇很长的文章链接,如果使用原始长链接,可能会导致显示不完整或影响美观,而通过短链接接口生成的短链接则能很好地解决这个问题。
另外,一些营销活动中也常使用短链接接口,方便跟踪链接的点击次数、来源等数据,以评估活动效果。比如,一个电商平台的促销活动链接,通过短链接接口生成短链接后,可以更清晰地了解不同渠道带来的流量和转化情况。
下面是Java实现的具体参数,可供参考:
》》》》欢迎免费试用短链接接口服务《》《《
接口地址:https://market.aliyun.com/apimarket/detail/cmapi00066810#sku=yuncode6081000002
public static void main(String[] args) {
String host = "https://kzshorturl.market.alicloudapi.com";
String path = "/api/shortUrl/create";
String method = "POST";
String appcode = "你自己的AppCode";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
Map<String, String> bodys = new HashMap<String, String>();
bodys.put("longUrl", "longUrl");
bodys.put("length", "length");
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.println(response.toString());
//获取response的body
//System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
具体返回参数示例,如下:
{
"msg": "成功",
"success": true,
"code": 200,
"data": {
"orderNo": "202407181637378049652",
"shortUrlHTTPS": "https://j1q.cn/47ya",
"shortUrl": "j1q.cn/47ya", 短链接地址
"shortUrlHTTP": "http://j1q.cn/47ya",
"longUrl": "http://www.baidu.com",
"urlKey": "47ya"
}
}
标签:Java,String,示例,aliyun,接口,https,com,链接
From: https://blog.csdn.net/loosenivy/article/details/140561863