10、长链接转短链接

1、概述

短网址的好处众多,便于记忆,占用字符少等,现在市面上出现了众多的将长网址转变为短网址的方法,但是由于他们都是小的公司在幕后运营,所以很不靠谱,面对随时关闭服务的可能,这样也导致我们将转换好了的短网址也会失效失链!那么怎样才能使转换好了的短网址永久有效呢?

2、利用微信公众号接口实现长链接转短链接

开发者用于生成二维码的原链接(商品、支付二维码等)太长导致扫码速度和成功率下降,将原长链接通过此接口转成短链接再生成二维码将大大提升扫码速度和成功率。微信公众号平台提供的长链接转短链接接口是:

http请求方式: POST
https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN

参数说明

参数是否必须说明
access_token调用接口凭证
action此处填long2short,代表长链接转短链接
long_url需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url

调用举例

curl -d "{\"action\":\"long2short\",\"long_url\":\"http://wap.koudaitong.com/v2/showcase/goods?alias=128wi9shh&spm=h56083&redirect_count=1\"}" "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN"

返回说明,正常情况下,微信会返回下述JSON数据包给公众号:

{"errcode":0,"errmsg":"ok","short_url":"http:\/\/w.url.cn\/s\/AvCo6Ih"}

代码参考

/// <summary>
/// 长链接转短链接 /// </summary> /// <param name="longUrl">长链接</param> /// <returns></returns> [HttpPost] [ValidateInput(false)] [LoginAuthorize] public ActionResult GetShortUrl(string longUrl) { WeixinOfficialAccountEntity currentWeixinOfficialAccountEntity = RDIFrameworkService.Instance.WeixinBasicService.GetCurrentOfficialAccountEntity(ManageProvider.Provider.Current()); string token = currentWeixinOfficialAccountEntity.AccessToken; //微信请求地址 string url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" + token; //请求的json参数 string data = "{\"action\":\"long2short\",\"long_url\":\"" + longUrl + "\"}"; string ret = string.Empty; try { byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data); //转化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url)); webReq.Method = "POST"; webReq.ContentType = "application/json"; webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); var ce = response.ContentEncoding; StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")); ret = sr.ReadToEnd(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { } //正常情况下,微信会返回下述JSON数据包给公众号: //{"errcode":0,"errmsg":"ok","short_url":"http:\/\/w.url.cn\/s\/AvCo6Ih"} string errcode = "";//错误码。 string errmsg = "";//错误信息。 string short_url = "";//短链接。 //解析响应信息 if (!string.IsNullOrWhiteSpace(ret)) { JObject jo = (JObject)JsonConvert.DeserializeObject(ret); errcode = jo["errcode"].ToString();//错误码。 errmsg = jo["errmsg"].ToString();//错误信息。 short_url = jo["short_url"].ToString();//短链接。 } return Content(new JsonMessage { Success = true, Data = short_url, Type = ResultType.Success, Message = errmsg }.ToString()); }

3、使用效果参考

长链接转短链接界面


生成的短链接效果

上面的界面我们把链接地址http://blog.rdiframework.net/article/190通过我们提供的长链接转短链接界面功能转成了短链接https://w.url.cn/s/ALO1xZC

转载于:https://www.cnblogs.com/zhipeng007/p/11004656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值