发布日期:2017-04-11 00:09:20

我们在新浪微博和腾讯微博中会发现它们用的链接都是短链接。sinaurl.cn和url.cn等。阿里淘宝都有相应的短链接。这些链接都很短,但是最终又会重定向到原本的长链接网址上去。

优点就是链接短小。缺点就是不能直观的判断到底是链接到什么网站上去(有些网站就利用这做一些欺骗性的链接)。

其实现原理比较简单:将原先网址通过算法转化成有62个字符组成的一个短链接。62个字符为0-9a-zA-Z.

数据库只有两个字段seq(自增长数字)和url(数字的url地址,建立索引)。

  用户输入一个url地址,查询表是否包含此url,如果存在,则返回seq的数字,

  如果不存在,则插入数据库,得到一个新增加的自增seq数字,为了缩短数字占用的字符数,我们可以把abc等字母的大小写用上。这样10个数 字,26个小写字母,26个大小字母就组成了一个62进制了。比如数字10000000000(100亿)转换后就是aUKYOA,只有6位了,这样就能 缩短很多的网址了。

比如下面有两种语言的实现:

<?php
//十进制转到其他制
function dec2any( $num, $base=62, $index=false ) {
    if (! $base ) {
        $base = strlen( $index );
    } else if (! $index ) {
        $index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
    }
    $out = "";
    for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
        $a = floor( $num / pow( $base, $t ) );
        $out = $out . substr( $index, $a, 1 );
        $num = $num - ( $a * pow( $base, $t ) );
    }
    return $out;
}

function any2dec( $num, $base=62, $index=false ) {
    if (! $base ) {
        $base = strlen( $index );
    } else if (! $index ) {
        $index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base );
    }
    $out = 0;
    $len = strlen( $num ) - 1;
    for ( $t = 0; $t <= $len; $t++ ) {
        $out = $out + strpos( $index, substr( $num, $t, 1 ) ) * pow( $base, $len - $t );
    }
    return $out;
}

?>
public static string[] ShortUrl(string url)
        {
            //可以自定义生成MD5加密字符传前的混合KEY
            string key = "Leejor";
            //要使用生成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 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");

            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;
        }

这里列出几个短链接服务商:

  • 百度的 http://dwz.cn/
  • http://www.ft12.com/

接入方法:

TXT格式短网址API接口

接口:http://u6.gg/api.php?url=urlencode('要缩短的网址')

例如:http://u6.gg/api.php?url=http://www.baidu.com

返回:http://u6.gg/baidu

JSON格式短网址API接口

说明:format为json

例如:http://u6.gg/api.php?format=json&url=http://www.baidu.com

返回:{"url":"http://u6.gg/baidu","err":""}

JSONP格式短网址API接口

说明:format为jsonp

例如:http://u6.gg/api.php?format=jsonp&url=http%3A%2F%2Fwww.baidu.com%2F&callback=callbackname

返回:callbackname({"url":"http://u6.gg/baidu","err":""})

实验:

  • 使用一个长链接产生后的短链接进行再次生成短链接,问能不能正确导向原来的长链接地址?能

参阅资料:

  • 大家对短网址服务的前景怎么看?短网址服务商该怎么做?https://www.zhihu.com/question/19578265
  • 短网址服务搭建 http://www.cnblogs.com/aspnethot/articles/3492253.html
  • 网址缩短程序是怎么做的 http://www.cnblogs.com/sunli/archive/2010/03/25/1696183.html
  • 来个百度产生的短链接:http://dwz.cn/cTGri

发表评论