Goo.gl的使用方法

Google推出短网址服务goo.gl已经很久了,但是始终没有发布器API接口。只能通过Google的toolbar里的分享生成。关于Google IE 工具栏http://goo.gl/YFFV及Google Firefox工具栏http://goo.gl/wIDU的安装方法这里就不说了,还有就是Chrome插件 http://goo.gl/PiyX 这些都可以自行安装,使用起来也很方便的。还有其他的如FireFox插件http://goo.gl/BkMJ,googl-bookmarklet等。

而我们真正想实现的功能如下:(一个简单的输入框,一个按钮,然后生成链接)

网上找了一下,大致的方法:C#版

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Web;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
 
namespace GoogleUrlShortenerDotNetTest
{
    class GoogleUrlShortenerDotNet
    {
        private const string m_postFormat = "&user=toolbar@google.com&url={0}&auth_token={1}";
 
        public static string Shorten(string url)
        {
            string token = GenerateAuthToken(url);
            string post = String.Format(m_postFormat, HttpUtility.UrlEncode(url), token);
 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://goo.gl/api/url");
            request.ServicePoint.Expect100Continue = false;
            request.Method = "POST";
            request.UserAgent = "toolbar";
            request.ContentLength = post.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Cache-Control", "no-cache");
 
            using (Stream requestStream = request.GetRequestStream())
            {
                byte[] postBuffer = Encoding.ASCII.GetBytes(post);
                requestStream.Write(postBuffer, 0, postBuffer.Length);
            }
 
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader responseReader = new StreamReader(responseStream))
            {
                string json = responseReader.ReadToEnd();
                return Regex.Match(json, @"{""short_url"":""([^""]*)""}").Groups[1].Value;
            }
        }
 
        static string GenerateAuthToken(string b)
        {
            int i = __e(b);
 
            i = i >> 2 & 1073741823;
            i = i >> 4 & 67108800 | i & 63;
            i = i >> 4 & 4193280 | i & 1023;
            i = i >> 4 & 245760 | i & 16383;
 
            int h = __f(b);
            int k = (i >> 2 & 15) << 4 | h & 15;
            k |= (i >> 6 & 15) << 12 | (h >> 8 & 15) << 8;
            k |= (i >> 10 & 15) << 20 | (h >> 16 & 15) << 16;
            k |= (i >> 14 & 15) << 28 | (h >> 24 & 15) << 24;
 
            return "7" + __d(k);
        }
 
        static int __c(int a, int b, int c)
        {
            long l = 0;
            l += (a & 4294967295);
            l += (b & 4294967295);
            l += (c & 4294967295);
 
            return (int)l;
        }
 
        static int __c(int a, int b, int c, int d)
        {
            long l = 0;
            l += (a & 4294967295);
            l += (b & 4294967295);
            l += (c & 4294967295);
            l += (d & 4294967295);
 
            return (int)l;
        }
 
        static string __d(int l)
        {
            string ll = l.ToString();
            string m = (l > 0 ? l : l + 4294967296).ToString();
            bool n = false;
            int o = 0;
 
            for (int p = m.Length - 1; p >= 0; --p)
            {
                int q = Int32.Parse(m[p].ToString());
 
                if (n)
                {
                    q *= 2;
                    o += (int)Math.Floor((double)q / 10) + q % 10;
                }
                else
                {
                    o += q;
                }
 
                n = !n;
            }
 
            int mm = o % 10;
 
            o = 0;
 
            if (mm != 0)
            {
                o = 10 - mm;
 
                // In the original js, l.length was undefined...
                //if (ll.Length % 2 == 1)
                //{
                //    if (o % 2 == 1) o += 9;
                //    o /= 2;
                //}
            }
 
            m = o.ToString();
            m += ll;
 
            return m;
        }
 
        static int __e(string l)
        {
            int m = 5381;
            for (int o = 0; o < l.Length; o++)
            {
                m = __c(m << 5, m, (int)l[o]);
            }
            return m;
        }
 
        static int __f(string l)
        {
            int m = 0;
            for (int o = 0; o < l.Length; o++)
            {
                m = __c(l[o], m << 6, m << 16, -m);
            }
            return m;
        }
    }
}

以上代码仅供参考~

文章标签:

引用地址:http://www.biaodianfu.com/goo-gl.html

2 条关于“Goo.gl的使用方法”的回复

  1. amorphobia 说:

    博主请问怎样在博客留言中自动使用这个短网址呢?可以的话请用邮箱回复我,谢谢~!

发表评论




想让您的头像与众不同,请注册Gravatar