Google URL Kısaltıcısı, C#

Google URL Shortener, C#

Merhabalar,

Google’un hizmete sunduğu URL kısaltıcısısı, Goo.gl appspot üzerinden API desteği vermeye başladı. Hatta başlayalı bayağı oldu ama ben genede yeni bir habermiş gibi vereyim :) .

Goo.gl’dan kısaltmak istediğimiz adrese (http://ggl-shortener.appspot.com/?url=http://kaansengul.com) gidip, bize döndürdüğü nesneyi JSON objesi gibi okuyup, JSON.Net kullanarak sonucu basit bir şekilde elde edebiliyoruz:

        private string short(string url)
        {
            try
            {

                WebClient client = new WebClient();
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                string jsonText = new StreamReader(client.OpenRead("http://ggl-shortener.appspot.com/?url=" + url)).ReadToEnd();

                Googl gShortUrl = JsonConvert.DeserializeObject(jsonText, typeof(Googl)) as Googl;

                return gShortUrl.short_url;
            }
            catch
            {
                return null;
            }
        }
    }

    public class Googl
    {
        public string short_url { get; set; }
    }

Bu yöntemi kullanarak yaptığım basit WinForms uygulamasına buradan erişebilirsiniz.

Google URL Shortener, C#Projeyi ziyaret et

Google URL Shortener, C#

Hi,

Probably you know that, Google launched its new service, URL Shortener (Goo.gl) and started to give the API support over appspot.

Simply, we can shorten URLs via Goo.gl using C#. First we request (http://ggl-shortener.appspot.com/?url=http://kaansengul.com) the link that we want to make it short, then it returns a string that in the format of JSON and finally deserialing it with JSON.Net gives the result:

        private string short(string url)
        {
            try
            {

                WebClient client = new WebClient();
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                string jsonText = new StreamReader(client.OpenRead("http://ggl-shortener.appspot.com/?url=" + url)).ReadToEnd();

                Googl gShortUrl = JsonConvert.DeserializeObject(jsonText, typeof(Googl)) as Googl;

                return gShortUrl.short_url;
            }
            catch
            {
                return null;
            }
        }
    }

    public class Googl
    {
        public string short_url { get; set; }
    }

Using this function I made a simple WinForms application and published here.

Google URL Shortener, C#