日期:2014-05-18  浏览次数:21009 次

新浪微博的地址加密方式是什么?
在新粮微博里插入一个视频地址或者音频地址,会自动将地址变成类似这样的http://t.cn/h5hrB7,请问这是用的什么加密方式,能将长地址变成这么短?

------解决方案--------------------
这个方法是新浪自己定的,除非开发API给你,否则没有办法。
google 也有这种功能 http://goo.gl/

相关API调用
C# code

        /// <summary>
        /// 调用Google API 生成短网址
        /// </summary>
        /// <param name="longUrl">长地址</param>
        /// <returns>短网址</returns>
        public static string GetShortUrl(string longUrl)
        {
            CookieContainer cc = new CookieContainer();
            string url = "http://goo.gl/api/shorten";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cc;
            string data = "url=" + HttpUtility.UrlEncode(longUrl) + "&security_token=";
            request.ContentLength = data.Length;
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
            writer.Write(data);
            writer.Flush();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "utf-8"; //默认编码
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            data = reader.ReadToEnd();
            response.Close();
            return data;
        }