News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Oct 20 2018 12:09PM     Sanjay
  0 Comments    3477 Views  
Shortening the large bulk url to short by using Google URL Shortener.
Before implementing the bulk url shortener, you need to create an API key for Google API Web Request. 

Follow the below steps to create Google API Key

1) Go to the following link 
  https://developers.google.com/url-shortener/v1/getting_started#APIKey
2) Click on "GET A KEY, Select or Create a new project.




3) Now Click on "Enable API". (Sample API Key :  AIzaSyAvjD359ibDC-*****S8A73ZdTZ*****)


Now, You are ready to start developing with Google URL Shortener API.

class Program
    {        
        private const string key = "AIz****YnrVv*****iBnHfv5OIi****CxHh0";
        static List<string> shortUrlList = new List<string>();

        static void Main(string[] args)
        {
            var ds = new DataSet();
            string connectionString = ConfigurationManager.ConnectionStrings["DBString"].ConnectionString;
            using (var conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("get_log_url", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    var adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds);
                }
            }

            if (ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string longUrl = dr["longurl"].ToString();
                    string shortUrl = GoogleUrlShortener(longUrl);
                }
            }

     
        }

        public static string GoogleUrlShortener(string longUrl)
        {            
            string finalURL = "";
            string post = "{\"longUrl\": \"" + longUrl + "\"}";
            string shortUrl = longUrl;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + key);
            try
            {
                request.ServicePoint.Expect100Continue = false;
                request.Method = "POST";
                request.ContentLength = post.Length;
                request.ContentType = "application/json";
                request.Headers.Add("Cache-Control""no-cache");
                using (Stream requestStream = request.GetRequestStream())
                {
                    byte[] postBuffer = Encoding.ASCII.GetBytes(post);
                    requestStream.Write(postBuffer, 0, postBuffer.Length);
                }
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader responseReader = new StreamReader(responseStream))
                        {
                            string json = responseReader.ReadToEnd();

                            JObject jsonResult = JObject.Parse(json);
                            finalURL = jsonResult["id"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(" Success -- > " + ex.StackTrace);
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
            return finalURL;
        }
}

Result : 

Long URL : http://thedevelopertips.com/DotNet/ASPDotNet/Bind-Gridview-from-CSV-file.aspx?Id=115
Short URL : https://goo.gl/g1ek1U

Hope, This article helps you to shortning bulk long url. 

BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: