file_get_contents() in asp.net/C#

The URL shortening service http://bit.ly has a really simple and convient API that lets just get a short url like this.

http://bit.ly/api?url=http://www.myreallylong.com/url/with/lots?of=crap

I’ve used this in a wordpress installation with PHP before and there i could just do.

$twitter_url .= file_get_contents('http://bit.ly/api?url=' . get_permalink());

Simple and clean. But today i wanted to do the same thing with asp.net and found myself kind of lost. At last i came up with this, not as simple solution. But it works.

string shortUrl = new StreamReader(WebRequest.Create("http://bit.ly/api?url=http://fragor.ohsohightech.se" + Url.Action("Question", new { number = question.Number, title = question.Title.ConvertTextToUrl() })).GetResponse().GetResponseStream()).ReadToEnd();

Leave a Reply