Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

NO CAPTCHA, Shorten long URLs with goo.gl and get the long behind the short one !!!

Hello HF,

this is my first Thread and here i would like to release my Source!
With this code you can shorten any URL to http://goo.gl/, without Captcha!
Also you can get the long URL behind the shorten one!

This is all is via Webrequest.

Enjoy the Code and please give credits to me, if you use it !

Code to shorten a long URL:

Code
Public Function shortenurl(ByVal long_uri As String)
        Dim postdata As String = "{" & Chr(34) & "longUrl" & Chr(34) & ":" & Chr(34) & long_uri & Chr(34) & "}"
        Dim Request As HttpWebRequest = CType(WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url"), HttpWebRequest)
        Request.Method = "POST"
        Request.ContentType = "application/json"
        Dim Post As String = postdata
        Dim byteArray() As Byte = Encoding.UTF8.GetBytes(Post)
        Request.ContentLength = byteArray.Length
        Dim DataStream As Stream = Request.GetRequestStream()
        DataStream.Write(byteArray, 0, byteArray.Length)
        DataStream.Close()

        Dim Response As HttpWebResponse = Request.GetResponse()
        DataStream = Response.GetResponseStream()
        Dim reader As New StreamReader(DataStream)
        Dim ServerResponse As String = reader.ReadToEnd()
        reader.Close()
        DataStream.Close()
        Response.Close()

        Dim res As String = ServerResponse
        Dim result As String = res.Split(":")(2) & ":" & res.Split(":")(3)
        result = result.Substring(0, result.Length - 13)
        result = result.Remove(0, 2)
        Return result
    End Function 'Coded by The Expert

Code to get the long URL from the short one:

Code
Public Function getlongurl(ByVal long_uri As String)
        Dim web As New Net.WebClient
        Dim res As String
        res = web.DownloadString("https://www.googleapis.com/urlshortener/v1/url?shortUrl=" & long_uri)
        Dim result As String = res.Split(":")(4) & ":" & res.Split(":")(5)
        result = result.Substring(0, result.Length - 12)
        result = result.Remove(0, 2)
        Return result
    End Function 'Coded by The Expert

Thank you HF, you are so great !


~The Expert
Yeah i've tried doing this before, never got it. Great release! Totally using it!

--

So I just tested it out, and it works fine. This is good for a mass URL shortener.
(May 10, 2013 02:04 PM)Foursenal Wrote:  Yeah i've tried doing this before, never got it. Great release! Totally using it!

--

So I just tested it out, and it works fine. This is good for a mass URL shortener.

Thank you for your feedback, after 120 views at least 1 post :)
thank you for this
its awesome
good job man
(May 10, 2013 11:57 PM)k@$f Wrote:  thank you for this
its awesome
good job man

You are welcome, enjoy the Source :)
Thank you man..

I was looking for something like this, i will mess with the code if you don't mind :p!
(May 16, 2013 08:57 AM)Zayed Wrote:  Thank you man..

I was looking for something like this, i will mess with the code if you don't mind :p!

No Problem, you are Welcome !

~The Expert
Nice job, but a little convoluted, you can use a webclient for both functions and make it all a little easier to read to boot:

Code
Private Function GetShortUrl(ByVal longurl As String) As String
  Using wc As New Net.WebClient
    wc.Proxy = Nothing
    wc.Headers.Add("Content-Type", "application/json")
    Dim response = wc.UploadString("https://www.googleapis.com/urlshortener/v1/url", String.Format("{{""longUrl"":""{0}""}}", longurl))

    Return Strings.Split(Strings.Split(response, """id"": """)(1), """,")(0)
  End Using
End Function

Private Function GetLongUrl(ByVal shorturl As String) As String
  Using wc As New Net.WebClient
    wc.Proxy = Nothing
    Dim response = wc.DownloadString(String.Format("https://www.googleapis.com/urlshortener/v1/url?shortUrl={0}", shorturl))
    Return Strings.Split(Strings.Split(response, "longUrl"": """)(1), """,")(0)
  End Using
End Function
(May 16, 2013 10:03 AM)carrot223 Wrote:  Nice job, but a little convoluted, you can use a webclient for both functions and make it all a little easier to read to boot:

Code
Private Function GetShortUrl(ByVal longurl As String) As String
  Using wc As New Net.WebClient
    wc.Proxy = Nothing
    wc.Headers.Add("Content-Type", "application/json")
    Dim response = wc.UploadString("https://www.googleapis.com/urlshortener/v1/url", String.Format("{{""longUrl"":""{0}""}}", longurl))

    Return Strings.Split(Strings.Split(response, """id"": """)(1), """,")(0)
  End Using
End Function

Private Function GetLongUrl(ByVal shorturl As String) As String
  Using wc As New Net.WebClient
    wc.Proxy = Nothing
    Dim response = wc.DownloadString(String.Format("https://www.googleapis.com/urlshortener/v1/url?shortUrl={0}", shorturl))
    Return Strings.Split(Strings.Split(response, "longUrl"": """)(1), """,")(0)
  End Using
End Function

Yes your Code is easier, thanks for improving it !


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by joshythejew101Dec 29, 2020 07:57 PM 2 173 Dec 29, 2020 07:57 PM
Last Post: joshythejew101
[C#] by AstateOct 21, 2020 11:14 AM 3 184 Oct 21, 2020 11:14 AM
Last Post: Scapula™
[Shell] by zanoucimMar 21, 2019 07:55 AM 1 243 Mar 21, 2019 07:55 AM
Last Post: Tantalus