Utilizar a API do bit.ly para gerar um url curto em C#
Hoje andei a dar uma olhadela à API do serviço bit.ly. Para utilizarmos basta mesmo criar uma conta, e com a key que gera para utilizarmos a API rapidamente fazemos qualquer brincadeira.
Segue um exemplo rápido de como gerar um link curto a partir da URL inserida.
<span style="color: blue;">string </span><span style="color: black;">username </span><span style="color: blue;">= </span><span style="color: darkred;">"username"</span><span style="color: gray;">;<br />
</span><span style="color: blue;">string </span><span style="color: black;">api </span><span style="color: blue;">= </span><span style="color: darkred;">"your_api_key"</span><span style="color: gray;">;<br />
</span><span style="color: blue;">using </span><span style="color: gray;">(</span><span style="color: black;">WebClient w </span><span style="color: blue;">= new </span><span style="color: black;">WebClient</span><span style="color: gray;">())<br />
</span><span style="color: black;">{<br />
</span><span style="color: blue;">string </span><span style="color: black;">LongUrl </span><span style="color: blue;">= </span><span style="color: darkred;">"http://blog.tiagosalgado.com"</span><span style="color: gray;">;<br />
</span><span style="color: blue;">string </span><span style="color: black;">bitLyUrl </span><span style="color: blue;">=string</span><span style="color: black;">.Format</span><span style="color: gray;">(</span><span style="color: darkred;">"http://api.bit.ly/v3/shorten?login={0}&apiKey={1}&uri={2}&format=txt"</span><span style="color: gray;">,</span><span style="color: black;">username</span><span style="color: gray;">,</span><span style="color: black;">api</span><span style="color: gray;">,</span><span style="color: black;">LongUrl</span><span style="color: gray;">);<br />
</span><span style="color: blue;">string </span><span style="color: black;">ShortUrl </span><span style="color: blue;">= </span><span style="color: black;">w.DownloadString</span><span style="color: gray;">(</span><span style="color: black;">bitLyUrl</span><span style="color: gray;">);<br />
</span><span style="color: black;">Console.Write</span><span style="color: gray;">(</span><span style="color: black;">ShortUrl</span><span style="color: gray;">);<br />
</span><span style="color: black;">Console.Read</span><span style="color: gray;">();<br />
</span><span style="color: black;">}</span>
E temos algo como isto: