Instantly share code, notes, and snippets.

Embed
What would you like to do?
Readable.io API Example Code - VB.NET
'=====================================================================
' Readable.io REST Post Test
' --------------------------
' Basic asp.net (VB.Net) 4 page with textbox, label and submit button
' Created: 25 May 2018
' Author: humanotics.co.uk
'=====================================================================
Imports System.Net
Imports System.Security.Cryptography
Partial Class Readable
Inherits System.Web.UI.Page
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'submit button on page
Dim strApiURL As Uri = New Uri("https://api.readable.io/api/text/")
Dim strApiKey As String = "XXX" 'API key goes in here
Dim lngRequestTime As Long = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds 'generate UNIX timestamp
Dim apiSignature As String = GetHash(strApiKey & lngRequestTime.ToString) 'Hash the APIkey and Timestamp once concatenated
Dim strText As String = TextBox1.Text 'get input from textbox on page
Dim wReq As HttpWebRequest = WebRequest.Create(strApiURL) 'create a httpwebrequest object
Dim postData As String = "text=" & strText 'generate post data
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) 'convert postdata to byte array
wReq.Method = "POST" 'Set POST method
wReq.ContentType = "application/x-www-form-urlencoded" 'set FORM post type (not JSON)
wReq.ContentLength = byteArray.Length 'set length of post request
wReq.Headers.Add("API_SIGNATURE", apiSignature) 'set api signature http header
wReq.Headers.Add("API_REQUEST_TIME", lngRequestTime.ToString) 'set api request time http header (UNIX timestamp)
'convert request to datastream object
Dim dataStream As IO.Stream = wReq.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = wReq.GetResponse() 'declare response object
dataStream = response.GetResponseStream() 'get response as datastream
Dim reader As New IO.StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd() 'convert datastream to string
'Output response from Readable.io to a label control on page
Label1.Text = (responseFromServer)
'Tidy up
reader.Close()
dataStream.Close()
response.Close()
End Sub
Shared Function GetHash(theInput As String) As String
'take the input string and generate a MD5 has of it
Using hasher As MD5 = MD5.Create()
Dim dbytes As Byte() =
hasher.ComputeHash(Encoding.UTF8.GetBytes(theInput))
Dim sBuilder As New StringBuilder()
For n As Integer = 0 To dbytes.Length - 1
sBuilder.Append(dbytes(n).ToString("X2"))
Next n
Return sBuilder.ToString()
End Using
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment