重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
短网址应用已经在全国各大微博上开始流行了起来。例如QQ微博的url.cn,新郎的sinaurl.cn等。
成都创新互联公司2013年至今,是专业互联网技术服务公司,拥有项目成都做网站、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元洪山做网站,已为上家服务,为洪山各地企业和个人服务,联系电话:028-86922220我们在QQ微博上发布网址的时候,微博会自动判别网址,并将其转换,例如:/tupian/20230522/sorry
为什么要这样做的,原因我想有这样几点:
其实以上三点纯属个人观点,因为在我接下来的部分项目中会应用到,所以就了解了一下,下面先来看看短网址映射算法的理论(网上找到的资料):
很简单的理论,我们并不一定说得到的URL是唯一的,但是我们能够取出4组URL,这样几乎不会出现太大的重复。
下面来看看程序部分:
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; }