How to have user input URL in google's URL Shortener?

288 Views Asked by At

So im making an a fun console app that the user types in a url and returns the shortened version of it. So when i state: storage= "{\"longUrl\":\"https://www.facebook.com/\"}"; //Store website/link URL instead of facebook.com how do i use urlString?

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Google.Apis.Urlshortener;
    using System.Net;
    using System.IO;

   namespace GoogleTest
   {
    class urlShortener
    {
        private string urlString; //Contains string of the URL
        private string nameOfurl; //Generic URL name
        private string shortenedURLstring;
        private string storage;

        public urlShortener(string nameOfurl, string urlString) //create new shortened URL
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            this.nameOfurl = nameOfurl;
            this.urlString = urlString;

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                storage= "{\"longUrl\":\"https://www.facebook.com/\"}"; //Store website/link URL 
                Console.WriteLine(storage);
                streamWriter.Write(storage);
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
                Console.WriteLine(responseText);
            }
        }
    }
}
0

There are 0 best solutions below