Create a gist now

Instantly share code, notes, and snippets.

What would you like to do? API Example Code - VB.NET
' REST Post Test
' --------------------------
' Basic (VB.Net) 4 page with textbox, label and submit button
' Created: 25 May 2018
' Author:
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("")
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)
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 to a label control on page
Label1.Text = (responseFromServer)
'Tidy up
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() =
Dim sBuilder As New StringBuilder()
For n As Integer = 0 To dbytes.Length - 1
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