技术思绪摘录旅行笔记
早期短链接广泛应用于图片上传网站,通过缩短网址URL链接字数,达到减少代码字符串的目的。更便于使用者引用网址,写入代码中。节省字符数空间。常见于网店图片分类的使用,因有字符限制,运用短链接,达到外链图片的目的,自微博盛行以来,在微博字数有限的特色下,短链接也盛行于微博网站,以节省字数,给博主发布更多文字的空间。

1、利用web站点的路由规则,做到 http://a.com/aUFBFr  能访问就行。配置方式如下。优先命中第一个路由

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "ShortLink",
                url: "{code}",
                defaults: new { controller = "URL", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    public class URLController : Controller
    {
        /// <summary>
        /// 短链接访问
        /// </summary>
        /// <param name="code">The code.</param>
        /// <returns></returns>
        public ActionResult Index(string code)
        {
            string longurl = RedisUtility.GetRedis<string>(code);
            Response.Redirect(longurl);
            return View();
        }
    }

2、生成【aUFBFr】这个短字符的算法:

这个算法根据长链接算出四个字符串,理论上同样长链接他的字符串是一样的,重复率非常低,此处算法会对比第一组,如果key存在且长链接不等,则继续对比下一个字符串,以此类推。

        /// <summary>
        /// 长链接加密成四个字符串  每个字符串重复几率都很低  同时四组重复的话  说明长链接是一样的
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static string[] ShortUrlEnCode(string url)
        {
            //可以自定义生成MD5加密字符传前的混合KEY
            string key = "CarsonYang";
            //要使用生成URL的字符
            string[] chars = new string[]{
                "a","b","c","d","e","f","g","h",
                "i","j","k","l","m","n","o","p",
                "q","r","s","t","u","v","w","x",
                "y","z","0","1","2","3","4","5",
                "6","7","8","9","A","B","C","D",
                "E","F","G","H","I","J","K","L",
                "M","N","O","P","Q","R","S","T",
                "U","V","W","X","Y","Z"
            };
            //对传入网址进行MD5加密
            string hex =Md5Hash(key + url);
            string[] resUrl = new string[4];
            for (int i = 0; i < 4; i++)
            {
                //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
                int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
                string outChars = string.Empty;
                for (int j = 0; j < 6; j++)
                {
                    //把得到的值与0x0000003D进行位与运算,取得字符数组chars索引
                    int index = 0x0000003D & hexint;
                    //把取得的字符相加
                    outChars += chars[index];
                    //每次循环按位右移5位
                    hexint = hexint >> 5;
                }
                //把字符串存入对应索引的输出数组
                resUrl[i] = outChars;
            }
            return resUrl;
        }


        /// <summary>
        /// 获取短链接
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public static string GetShortUrl(string url)
        {
            string[] codes = ShortUrlEnCode(url);
            for (int i = 0; i < 4; i++)
            {
                string code = codes[i];
                string longurl = RedisUtility.GetRedis<string>(code);
                if (!string.IsNullOrWhiteSpace(longurl) && longurl != url)
                {
                    continue;
                }
                if (string.IsNullOrWhiteSpace(longurl))
                {
                    RedisUtility.SetRedis<string>(code, url, DateTime.Now.AddHours(1));
                    return "http://a.com/" + code;
                }
                else
                {
                    return "http://a.com/" + code;
                }
            }
            return url;
        }

        public static void Test()
        {
            //长链接转短链接
            string url = "http://www.yangshaofeng.com?state=456456454655664654654654654654656546546544456456454655664654654654654654656546546544456456454655664654654654654654656546546544456456454655664654654654654654656546546544";
            Console.WriteLine(GetShortUrl(url));
        }

3、存储:此处利用了Redis,key就是短字符,value就是长链接。你也可以用数据库和其他速度快的存储方式。

4、最后结果:


CarsonIT 微信扫码关注公众号 策略、创意、技术

留下您的脚步

 

最近评论

查看更多>>

站点统计

总文章数:276 总分类数:18 总评论数:90 总浏览数:170.65万

精选推荐

阅读排行

友情打赏

请打开您的微信,扫一扫