Parsi Coders
How to adfly URL Shorting - Printable Version

+- Parsi Coders (
+-- Forum: Software Development Programming (
+--- Forum: C# Programming (
+--- Thread: How to adfly URL Shorting (/showthread.php?tid=3708)

How to adfly URL Shorting - idroo - 10-06-2014

Quote: Nasıl adfly URL Apı örnek kod kısa Devre ?

RE: How to adfly URL Shorting - Amin_Mansouri - 10-12-2014

Public Shared Function Shorten(url As String) As String
       Dim key As String = "$MYGOOGLEAPIKEY$"
       Dim post As String = "{""longUrl"": """ & url & """}"
       Dim shortUrl As String = url
       Dim request As HttpWebRequest = DirectCast(WebRequest.Create("" & key), HttpWebRequest)
           request.ServicePoint.Expect100Continue = False
           request.Method = "POST"
           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)
           End Using
           Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
               Using responseStream As Stream = response.GetResponseStream()
                   Using responseReader As New StreamReader(responseStream)
                       Dim json As String = responseReader.ReadToEnd()
                       shortUrl = Regex.Match(json, """id"": ?""(?<id>.+)""").Groups("id").Value
                   End Using
               End Using
           End Using
       Catch ex As Exception
           ' if Google's URL Shortner is down...
       End Try
       Return shortUrl
   End Function