Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 520

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 535

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 542

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 578

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/gathadam/public_html/wp-settings.php on line 18
Add ‘Tweet This’ button to your Asp.Net site. Twitter + Bit.ly url shortening
Previous    

Add ‘Tweet This’ button to your Asp.Net site. Twitter + Bit.ly url shortening

Adding your own ‘Tweet This’ buttons so your visitors can post to twitter (complete with URL shortening using Bit.ly) is really easy.

Just add a page ‘twitter.aspx’ to your site and remove all the HTML from the page.

Add this code:


Imports System.Net

Partial Public Class Twitter

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Request.QueryString("url") <> "" And Request.QueryString("com") <> "" Then

            TweetThis(Request.QueryString("url"), Request.QueryString("com"))

        End If

    End Sub

    Private Sub TweetThis(ByVal sIWTFURL As String, ByVal sComment As String)

        Dim sShortURL As String

        Dim sFullTweet As String

        sShortURL = ShortenURL(sIWTFURL)

        sFullTweet = sComment & " - " & sShortURL

        SentToTwitter(sFullTweet)

    End Sub

    Private Function ShortenURL(ByVal sURL As String) As String

        Dim sJSON As String

        Dim oWebClient As New WebClient

        Dim sBitlyURL As String

        Dim sShortURL As String

        sBitlyURL = "http://api.bit.ly/shorten?version=2.0.1&longUrl=" & Server.UrlEncode(sURL) & "&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07"

        sJSON = oWebClient.DownloadString(sBitlyURL)

        sJSON = Mid$(sJSON, InStr(sJSON, Chr(34) & "shortUrl" & Chr(34)) + 9)

        Do While sJSON.ToLower.StartsWith("http://") = False

            sJSON = Mid$(sJSON, 2)

        Loop

        sShortURL = sJSON.Split(Chr(34))(0)

        ShortenURL = sShortURL

    End Function

    Private Sub SentToTwitter(ByVal sComments As String)

        Dim sURL As String

        sURL = "http://twitter.com/home/?status=" & HttpUtility.UrlEncode(sComments)

        Response.Redirect(sURL, False)

    End Sub

End Class

Note: I am using the default Bit.ly login in the example, you should sign up for your own bit.ly account so you can track usage of your links.

Now you just need to add some javascript to your html pages:


<a href="#" onClick='javascript:PostToTwitter()'>Tweet This</a>

<script>
function PostToTwitter()
{
var sTweet = 'This is the default text that will appear in the tweet';
var ShareURL = window.location.href;

window.open('http://yoursite.com/twitter.aspx?url='+encodeURIComponent(ShareURL)+'&com='+encodeURIComponent(sTweet));
return false;
}
</script>


If you liked this, then subscribe to my RSS feed

5 comments

  1. Mark Aug 25

    This may sound simple, but how do you encrypt your bitly password for the twitter.aspx page?

  2. Gath Aug 25

    Hi Mark,

    You don’t need to, just change the line:
    sBitlyURL = “http://api.bit.ly/shorten?version=2.0.1&longUrl=” & Server.UrlEncode(sURL) & “&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07″

    replace:
    login=bitlyapidemo
    with
    login=yourbitlyname

    and:
    apiKey=R_0da49e0a9118ff35f52f629d2d71bf07
    with
    apiKey=YourBitlyAPIKey

    You can add a Server.UrlEncode() to the apiKEY, but there really is no need (I know, because that is what I am doing & it works so far).

  3. Greenville roofing Aug 28

    wow.. Thanks Gath. Its very simple. I was searching for a solution like this for last 2 days. Im going to ask our programmer to add this.

  4. Oliver Aug 31

    Hi, thanks for this. But what do I need to change so that it tweets when an image on my paged is clicked?

  5. Gath Aug 31

    Oliver - Sorry, I don’t understand. What exactly are you trying to do?