C#:生成短網址


 /// <summary>
    /// 短網址應用 ,例如QQ微博的url.cn   http://url.cn/2hytQx
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string[] ShortUrl(string url)
    {
        //可以自定義生成MD5加密字符傳前的混合KEY
        string key = "fooo_2016";
        //要使用生成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;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string[] Shor = ShortUrl(this.TextBox1.Text);


        // Response.Write( );  //得到值fAVfui
        //ShortUrl(http://www.jb51.net)[1];  //得到值3ayQry
        //ShortUrl(http://www.jb51.net)[2];  //得到值UZzyUr
        //ShortUrl(http://www.jb51.net)[3];  //得到值36rQZn

        for (int i = 0; i < Shor.Length; i++)
        {
            Response.Write("<br/>" + i + ">>" + Shor[i]);
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM