Hack Forums
[.NET] NO CAPTCHA, Shorten long URLs with goo.gl and get the long behind the short one !!! - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] NO CAPTCHA, Shorten long URLs with goo.gl and get the long behind the short one !!! (/showthread.php?tid=3469606)



NO CAPTCHA, Shorten long URLs with goo.gl and get the long behind the short one !!! - ๖ۣۜXilluX - May 10, 2013

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



* - Foursenal - May 10, 2013

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.


* - ๖ۣۜXilluX - May 10, 2013

(May 10, 2013 02:04 PM)4rsenal 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 :)


* - k@$f - May 10, 2013

thank you for this
its awesome
good job man


* - ๖ۣۜXilluX - May 10, 2013

(May 10, 2013 11:57 PM)k@$f Wrote:  thank you for this
its awesome
good job man

You are welcome, enjoy the Source :)


* - Zayed - May 16, 2013

Thank you man..

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


* - ๖ۣۜXilluX - May 16, 2013

(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


* - carrot223 - May 16, 2013

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



* - ๖ۣۜXilluX - May 16, 2013

(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 !