|
发表于 2013-10-16 16:18:13
|
显示全部楼层
字符是对应的,但按我们自己对应的规则来,跳转时能自动解析?
下面是我参照一网友的代码写的,请站长指正:- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using System.Globalization;
- //参考链接:http://blog.csdn.net/querdaizhi/article/details/6712291
- namespace yournamespace
- {
- class GetShortUrl
- {
- public static string GetShortUrls(string longurl)
- {
- string tem="";
- string[] temUrl=shortUrl(longurl);
- for (int i = 0; i < 4; i++)
- {
- tem += temUrl[i] + "|";
- }
- return tem.Substring(0, tem.Length - 1);
- }
- public static String[] shortUrl(String 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"
- };
- String sMD5EncryptResult = GetMD5(url);
- String hex = sMD5EncryptResult;
- String[] resUrl = new String[4];
- for (int i = 0; i < 4; i++)
- {
- String sTempSubString = hex.Substring(i * 8, 8);
- long lHexLong = 0x3FFFFFFF & long.Parse(sTempSubString, NumberStyles.HexNumber);
- String outChars = "";
- for (int j = 0; j < 6; j++)
- {
- long index = 0x0000003D & lHexLong;
- outChars += chars[(int)index];
- lHexLong = lHexLong >> 5;
- }
- resUrl[i] = outChars;
- }
- return resUrl;
- }
- private static string GetMD5(String input)
- {
- byte[] result = Encoding.Default.GetBytes(input);
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] output = md5.ComputeHash(result);
- return BitConverter.ToString(output).Replace("-", "");
- }
- }
- }
- 执行:
- MessageBox.Show(GetShortUrl.GetShortUrls("http://tech.sina.com.cn/i/2011-03-23/11285321288.shtml"));//生成4个可用字符串
复制代码 |
|