Generate Short URL From The Long URL Using C#

In this article, you will know that how to generate a short URL from a long URL in .NET using C#. Here I’m using the Bitly platform. Bitly is a link management platform. So let’s start it.

Steps You Need To Follow

  1. Signup in Bitly.
  2. Get API key.
    1. you can get your API key here https://app.bitly.com/settings/api/

Before start, you will need the above things first.

Here Is My Code

using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;

namespace ShortLink
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Enter Long URL");
            string LongURL;
            LongURL= Console.ReadLine();
            string ShortURL = Shorten(LongURL);
            Console.WriteLine();
            Console.WriteLine("Long URL: " + LongURL);
            Console.WriteLine("Short URL: " + ShortURL);

            Console.ReadLine();
        }
        public static string Shorten(string longUrl)
        {
            string apikey = "YOUR_API_KEY";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            var url = "https://api-ssl.bitly.com/v4/shorten";

            var httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Method = "POST";

            httpRequest.Headers["Authorization"] = "Bearer " + apikey;
            httpRequest.ContentType = "application/json";

            var data = new { long_url = longUrl, domain = "bit.ly" };

            using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
            {
                streamWriter.Write(JsonConvert.SerializeObject(data));
            }

            var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            var streamReader = new StreamReader(httpResponse.GetResponseStream());
            ShortLinkResponce result = JsonConvert.DeserializeObject<ShortLinkResponce>(streamReader.ReadToEnd());


            return result.link;
        }
        public class ShortLinkResponce
        {
            public DateTime created_at { get; set; }
            public string id { get; set; }
            public string link { get; set; }
            public object[] custom_bitlinks { get; set; }
            public string long_url { get; set; }
            public bool archived { get; set; }
            public object[] tags { get; set; }
            public object[] deeplinks { get; set; }
        }
    }
}

In the above code, in the main method, I’m asking for a long URL that I’m passing to the Shorten method. Using Shorten method I’m generating a short URL of a long URL. Shorten method is sending a request to the https://api-ssl.bitly.com/v4/shorten and sending the API key in the header and over a long URL in content. In response to the request, we will get the data like ShortLinkResponce class. After getting the response I’m returning a short URL and displaying both the URLs.

Final Output

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories