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

后端 未结 6 890
栀梦
栀梦 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

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

提交回复
热议问题