Shortening Your Long URLs with the TinyURL API and .NET

 

The world is becoming lazier. It gets simpler to do certain errands, and certain beforehand monotonous assignments presently finish for us. Innovation is extraordinary! With new innovation comes new arrangements, as on account of the present theme. Indeed, TinyURL has been around seemingly forever, however you might not have found out about it previously, or, you might have been considering how to abbreviate your long URLs rapidly as TinyURL does. Today, I will show you. It is very simple and not a ton of work, so how about we begin, will we?

Practical

Create a new C# or Visual Basic.NET Windows Forms Application and design your form to resemble Figure 1.

Figure 1: Design
Add the ShrinkURL method.
C#

   private string ShrinkURL(string strURL)
   {
      string URL;
      URL = "http://tinyurl.com/api-create.php?url=" +
         strURL.ToLower();
      System.Net.HttpWebRequest objWebRequest;
      System.Net.HttpWebResponse objWebResponse;
      System.IO.StreamReader srReader;
      string strHTML;
      objWebRequest = (System.Net.HttpWebRequest)System.Net
         .WebRequest.Create(URL);
      objWebRequest.Method = "GET";
      objWebResponse = (System.Net.HttpWebResponse)objWebRequest
         .GetResponse();
      srReader = new System.IO.StreamReader(objWebResponse
         .GetResponseStream());
      strHTML = srReader.ReadToEnd();
      srReader.Close();
      objWebResponse.Close();
      objWebRequest.Abort();
      return (strHTML);
   }


VB.NET

   Private Function ShrinkURL(ByVal strURL As String) As String
      Dim URL As String
      URL = "http://tinyurl.com/api-create.php?url=" + _
         strURL.ToLower
      Dim objWebRequest As Net.HttpWebRequest
      Dim objWebResponse As Net.HttpWebResponse
      Dim srReader As IO.StreamReader
      Dim strHTML As String
      objWebRequest = CType(Net.WebRequest.Create(URL), _
         Net.HttpWebRequest)
      objWebRequest.Method = "GET"
      objWebResponse = CType(objWebRequest.GetResponse(), _
         Net.HttpWebResponse)
      srReader = New IO.StreamReader(objWebResponse _
         .GetResponseStream)
      strHTML = srReader.ReadToEnd
      srReader.Close()
      objWebResponse.Close()
      objWebRequest.Abort()
      Return (strHTML)
   End Function

The ShrinkURL function generates a shortened URL with the help of the ‘api-create’ method in its URL. You supply the long URL that was entered in one of the Textboxes; then, you need to create WebRequest objects to obtain the returned shortened URL and a StreamReader object to interpret the URL and return a properly formed string to be returned to the calling method or procedure.

Add the code for the Process and Copy buttons.

C#

   private void btnProcess_Click(object sender, EventArgs e)
   {
      System.Threading.Thread.Sleep(1000);
      txtOutput.Text = ShrinkURL(txtURL.Text);
      txtURL.Text = "";
   }
   private void btnCopy_Click(object sender, EventArgs e)
   {
      Clipboard.SetText(txtOutput.Text);
   }

VB.NET

   Private Sub btnProcess_Click(sender As Object, e As EventArgs) _
         Handles btnProcess.Click
      Threading.Thread.Sleep(1000)
      txtOutput.Text = ShrinkURL(txtURL.Text)
      txtURL.Text = ""
   End Sub
   Private Sub btnCopy_Click(sender As Object, e As EventArgs) _
         Handles btnCopy.Click
      Clipboard.SetText(txtOutput.Text)
   End Sub

The Process button waits a second, and then adds the shrunken URL to the Output Textbox. The Copy button simply copies the URL to the Clipboard.

Figure 2 shows a long URL that was entered. Figure 3 shows the shortened URL.

Figure 2: Long URL

Figure 3: Short URL
End 
No fuss. I simply need to thank everybody for perusing my articles. A few articles are very long, some are short, however I do trust you advantage from them. My point with these articles is to assist you with learning crazy deceives, or fascinating things, or essentially discover some new information. I know what it resembles to battle and not realize what to do and where to go and what to search for. viastudy.com has helped to such an extent! I simply trust my thoughts and my experience assist you with welling.
#viastudy

Post a Comment

0 Comments