18

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

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

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

4

6 回答 6

23

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

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);
于 2008-12-14T03:49:21.910 回答
12

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

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

看起来它可以解决问题。

于 2008-12-14T03:34:28.040 回答
3

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

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

于 2008-12-14T03:13:00.017 回答
3

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

于 2008-12-14T03:34:29.880 回答
1

根据这篇文章,您可以像这样实现它:

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;
    }
}
于 2019-07-26T09:45:51.927 回答
0

这是我的实现版本:

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;
}
于 2019-07-26T08:53:33.037 回答