Imports System.Text.RegularExpressions
Console.WriteLine(GoogleUrlShortnerApi.Shorten("http://maxxerp.com/download/ABCDEDFghijklmnoopTest.pdf"))
Public Class GoogleUrlShortnerApi
Private Const key As String = "AIzaSyBdBMsQm-FYnv-lW5BHfJHNQnmHcQYJ8qs"
Public Shared Function Shorten(ByVal url As String) As String
Dim post As String = "{""longUrl"": """ & url & """}"
Dim shortUrl As String = url
Dim request As HttpWebRequest = CType(WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" & key), HttpWebRequest)
request.ServicePoint.Expect100Continue = False
request.ContentLength = post.Length
request.ContentType = "application/json"
request.Headers.Add("Cache-Control", "no-cache")
Using requestStream As Stream = request.GetRequestStream()
Dim postBuffer As Byte() = Encoding.ASCII.GetBytes(post)
requestStream.Write(postBuffer, 0, postBuffer.Length)
Using response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Using responseStream As Stream = response.GetResponseStream()
Using responseReader As StreamReader = New StreamReader(responseStream)
Dim json As String = responseReader.ReadToEnd()
shortUrl = Regex.Match(json, """id"": ?""(?<id>.+)""").Groups("id").Value
System.Diagnostics.Debug.WriteLine(ex.Message)
System.Diagnostics.Debug.WriteLine(ex.StackTrace)