【问题标题】:Using tinyurl.com in a .Net application ... possible?在 .Net 应用程序中使用 tinyurl.com ...可能吗?
【发布时间】:2010-09-26 20:06:34
【问题描述】:

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

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

这将自动创建一个 tinyurl 网址。有没有办法使用代码,特别是 ASP.NET 中的 C# 来做到这一点?

【问题讨论】:

    标签: c# .net asp.net api tinyurl


    【解决方案1】:

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

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

    【讨论】:

    • System.Net.Uri 需要使用哪个程序集?
    • 所以有一个更正,它的System.Uri 而不是System.Net.Uri
    • @user1512:我修复了命名空间。
    【解决方案2】:

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

        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】:

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

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

      【讨论】:

        【解决方案4】:

        请记住,如果您正在开发一个完整的应用程序,那么您将与 TinyURL 的 URL/API 方案建立一个非常具体的依赖关系。也许他们保证他们的 URL 不会改变,但值得一试

        【讨论】:

        • 我一定会让六年前的保罗知道
        【解决方案5】:

        根据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】:

          这是我的实现版本:

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

          【讨论】:

            猜你喜欢
            • 2011-04-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-27
            • 2019-07-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多