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

后端 未结 6 1031
栀梦
栀梦 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:11

    After doing some more research ... I stumbled upon the following code:

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

    Looks like it may do the trick.

提交回复
热议问题