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

后端 未结 6 996
栀梦
栀梦 2020-12-28 08:24

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

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

This will automatically crea

相关标签:
6条回答
  • 2020-12-28 09:04

    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

    0 讨论(0)
  • 2020-12-28 09:04

    Here my version of the implementation:

    static void Main()
    {
        var tinyUrl = MakeTinyUrl("https://stackoverflow.com/questions/366115/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;
    }
    
    0 讨论(0)
  • 2020-12-28 09:05

    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.

    0 讨论(0)
  • 2020-12-28 09:08

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 09:11

    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.

    0 讨论(0)
  • 2020-12-28 09:13

    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);
    
    0 讨论(0)
提交回复
热议问题