Using tinyurl.com in a .Net application ... possible?

12,083

Solution 1

You should probably add some error checking, etc, but this is probably the easiest way to do it:

System.Uri address = new System.Uri("http://tinyurl.com/api-create.php?url=" + YOUR ADDRESS GOES HERE);
System.Net.WebClient client = new System.Net.WebClient();
string tinyUrl = client.DownloadString(address);
Console.WriteLine(tinyUrl);

Solution 2

After doing some more research ... I stumbled upon the following code:

    public static string MakeTinyUrl(string url)
    {
        try
        {
            if (url.Length <= 30)
            {
                return url;
            }
            if (!url.ToLower().StartsWith("http") && !Url.ToLower().StartsWith("ftp"))
            {
                url = "http://" + url;
            }
            var request = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + url);
            var res = request.GetResponse();
            string text;
            using (var reader = new StreamReader(res.GetResponseStream()))
            {
                text = reader.ReadToEnd();
            }
            return text;
        }
        catch (Exception)
        {
            return url;
        }
    }

Looks like it may do the trick.

Solution 3

You have to call that URL from your code, then read back the output from the server and process it.

Have a look at the System.Net.WebClient class, DownloadString (or better: DownloadStringAsync) seems to be what you want.

Solution 4

Keep in mind if you're doing a full-scale app, that you're wiring in a pretty specific dependency to TinyURL's URL/API scheme. Maybe they have guarantees about their URL not changing, but it's worth checking out

Solution 5

According to this article, you could implement it like this:

public class TinyUrlController : ControllerBase
{
    Dictionary dicShortLohgUrls = new Dictionary();

    private readonly IMemoryCache memoryCache;

    public TinyUrlController(IMemoryCache memoryCache)
    {
        this.memoryCache = memoryCache;
    }

    [HttpGet("short/{url}")]
    public string GetShortUrl(string url)
    {
        using (MD5 md5Hash = MD5.Create())
        {
            string shortUrl = UrlHelper.GetMd5Hash(md5Hash, url);
            shortUrl = shortUrl.Replace('/', '-').Replace('+', '_').Substring(0, 6);

            Console.WriteLine("The MD5 hash of " + url + " is: " + shortUrl + ".");

            var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(604800));
            memoryCache.Set(shortUrl, url, cacheEntryOptions);

            return shortUrl;
        }
    }

    [HttpGet("long/{url}")]
    public string GetLongUrl(string url)
    {
        if (memoryCache.TryGetValue(url, out string longUrl))
        {
            return longUrl;
        }

        return url;
    }
}
Share:
12,083
mattruma
Author by

mattruma

I fell in love with computers when I got my first Commodore 64! Check me out on twitter at http://www.twitter.com/mattruma

Updated on July 09, 2022

Comments

  • mattruma
    mattruma almost 2 years

    I found the following code to create a tinyurl.com url:

    http://tinyurl.com/api-create.php?url=http://myurl.com
    

    This will automatically create a tinyurl url. Is there a way to do this using code, specifically C# in ASP.NET?