Using tinyurl.com in a .Net application ... possible?
Asked Answered
P

6

18

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?

Pitiless answered 14/12, 2008 at 3:7 Comment(0)
P
23

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);
Phosphide answered 14/12, 2008 at 3:49 Comment(3)
Which assembly do we need to use for System.Net.Uri ?Brady
so there's a correction, its System.Uri and not System.Net.UriBrady
@user1512: I fixed the namespace.Phosphide
P
12

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.

Pitiless answered 14/12, 2008 at 3:34 Comment(0)
A
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.

Asinine answered 14/12, 2008 at 3:13 Comment(0)
O
3

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

Organon answered 14/12, 2008 at 3:34 Comment(1)
I'll make sure to let Paul from six years ago knowOrganon
C
1

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;
    }
}
Cowrie answered 26/7, 2019 at 9:45 Comment(0)
M
0

Here my version of the implementation:

static void Main()
{
    var tinyUrl = MakeTinyUrl("https://mcmap.net/q/653619/-using-tinyurl-com-in-a-net-application-possible");

    Console.WriteLine(tinyUrl);

    Console.ReadLine();
}

public static string MakeTinyUrl(string url)
{
    string tinyUrl = url;
    string api = " the api's url goes here ";
    try
    {
        var request = WebRequest.Create(api + url);
        var res = request.GetResponse();
        using (var reader = new StreamReader(res.GetResponseStream()))
        {
            tinyUrl = reader.ReadToEnd();
        }
    }
    catch (Exception exp)
    {
        Console.WriteLine(exp);
    }
    return tinyUrl;
}
Minium answered 26/7, 2019 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.