我发现下面的代码来创建tinyurl.com URL:

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

这将自动创建TinyURL的网址。有一种方法来做到这一点使用代码,具体地C#中ASP.NET?

有帮助吗?

解决方案

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

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

其他提示

做一些进一步的研究之后......我偶然发现了下面的代码:

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

看起来它可以达到目的。

请记住,如果你正在做一个全面的应用程序,那你在一个非常具体的依赖关系TinyURL的的URL / API方案接线。也许他们有他们的网址不改变的保证,但它是值得一试。

您必须调用您的代码,URL,然后读回从服务器过程中的输出。

有一个在所述 System.Net.WebClient 类, DownloadString (或更好地: DownloadStringAsync )似乎是什么你想要的。

根据文章,你可以实现这样的:

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

下面我的版本的执行情况:

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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top