代码之家  ›  专栏  ›  技术社区  ›  mattruma

使用tinyurl.com。网络应用程序。…可能?

  •  18
  • mattruma  · 技术社区  · 16 年前

    我找到了以下代码来创建tinyurl.com网址:

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

    这将自动创建一个tinyurl url。有没有一种方法可以使用代码来实现这一点,特别是ASP中的C#。网?

    6 回复  |  直到 16 年前
        1
  •  23
  •   mcrumley    12 年前

    您可能应该添加一些错误检查等,但这可能是最简单的方法:

    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);
    
        2
  •  12
  •   mattruma    16 年前

    在做了更多的研究之后。..我偶然发现了以下代码:

        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;
            }
        }
    

    看起来它可能会奏效。

        3
  •  3
  •   Michael Stum    16 年前

    您必须从代码中调用该URL,然后读回服务器的输出并对其进行处理。

    看看 System.Net.WebClient 类, DownloadString (或更好: DownloadStringAsync )这似乎就是你想要的。

        4
  •  3
  •   Ana Betts    16 年前

    如果你正在做一个全面的应用程序,请记住,你正在连接一个相当特定的依赖TinyURL的URL/API方案。也许他们保证他们的网址不会改变,但值得一试

        5
  •  1
  •   Tobias Tengler    6 年前

    根据 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;
        }
    }
    
        6
  •  0
  •   Mselmi Ali    6 年前

    以下是我的实现版本:

    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;
    }