How To Use TinyURL API In VB.NET

TinyURL Icon Ultimate Programming TutorialsTinyURL is a URL shortening service a web service that provides short URLs for redirection of long URLs. This can be achieved very easily in PHP, I posted a PHP snippet about this you could reach it from here. Last year I created a TinyURL shortener I named it TinyURL Desktop App you can download it from here if you want. I found the source code of TinyURL Desktop App just now that's why I am writing this hehe. I used Devcomponents in my TinyURL Desktop App also I didn't handle exceptions because I was a noob then :0.

Let's create it, add 3 Buttons and two Textboxes by the way arrange to look like this :
Tinyurl desktop app

The Buttons and their usages : 
  • Button 1 - Create TinyURL
  • Button 2 - Copy To Clipboard
  • Button 3 - Refresh
Button 1 code :
    'checking the network availability
        If My.Computer.Network.IsAvailable Then
            'creating the webrequest point
            Dim Request As WebRequest = DirectCast(WebRequest.Create("http://tinyurl.com/api-create.php?url=" + TextBox1.Text), HttpWebRequest)
            'defining the response
            Dim Response As HttpWebResponse = DirectCast(Request.GetResponse, HttpWebResponse)
            'getting the response
            Dim Stream As New StreamReader(Response.GetResponseStream)
            'writing the response(shortened URL)
            Dim ShortURL As String = Stream.ReadToEnd
            TextBox2.Text = ShortURL
        Else
            MsgBox("Please Connect To The Internet And Try Again", MsgBoxStyle.Critical)
        End If
Button 2 code :
     'copying...
        Clipboard.SetText(TextBox2.Text)
        MsgBox("Sucessfully Copied To Clipboard")
Button 3 code :
TextBox1.Text = "http://"
        TextBox2.Text = " "
        TextBox2.Enabled = False
Full source code :
Imports System.Net
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'checking the network availability
        If My.Computer.Network.IsAvailable Then
            'creating the webrequest point
            Dim Request As WebRequest = DirectCast(WebRequest.Create("http://tinyurl.com/api-create.php?url=" + TextBox1.Text), HttpWebRequest)
            'defining the response
            Dim Response As HttpWebResponse = DirectCast(Request.GetResponse, HttpWebResponse)
            'getting the response
            Dim Stream As New StreamReader(Response.GetResponseStream)
            'writing the response(shortened URL)
            Dim ShortURL As String = Stream.ReadToEnd
            TextBox2.Text = ShortURL
        Else
            MsgBox("Please Connect To The Internet And Try Again", MsgBoxStyle.Critical)
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'copying...
        Clipboard.SetText(TextBox2.Text)
        MsgBox("Successfully Copied To Clipboard")
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "http://"
        TextBox2.Text = " "
        TextBox2.Enabled = False
    End Sub
End Class
Testing... :
TextBox1 is for the original link and TextBox2 is for the shortened link.
tinyurl api

I hope you liked this tutorial.

6 comments

thanks a lot brother.
i love you

Reply

i got two error

1. Error 1 Namespace declaration must start with 'xmlns'.
2. Error 3 'Private Sub Button1_Click(sender As Object, e As System.EventArgs)' has multiple definitions with identical signatures.

Reply
Anonymous mod

That is your errors. You have two / more subs named Button1_Click. Delete one. Don't put in namespace. You just copied and pasted. Don't.

Reply

How can i generate shortner url in bulk?

Reply

Post a Comment

Note: Only a member of this blog may post a comment.