CCJ web service VB.NET example

The following VB.NET code example enables you to easily get up and running with the CCJ web service. Simply use the authentication credentials supplied to you on sign up which we have made available to you in the My Account section.

You will likely prefer to use one of of the available OAuth libraries to handle client authentication. The OAuth library used in the following example is listed separately.

Whilst our code examples demonstrate the breadth of our capabilities, all of our examples are created using demo data and do not contain real or live personal data relating to data subjects.

´╗┐Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.IO
Imports System.Net
Imports System.Web.Script.Serialization
 
' note that my project name was called CCJ_VB
Imports CCJ_VB.OAuth
 
 
Namespace Tracesmart.VBNet.Api
    Public Class CountyCourtJudgementService
 
        ' Your consumer key and secret as supplied by Tracesmart
        Private Const ConsumerKey As String = "b8e601941ad09b9478ac76c25b3bb46e10b8bdad"
        Private Const ConsumerSecret As String = "3065d6fa76t2da717bc5765388a8795a45245583"
 
        Public Sub MakeTestRequest()
            Dim parameters As IDictionary(Of String, String) = New Dictionary(Of String, String)() From { _
             {"title", "Ms"}, _
             {"forename", "Eveline"}, _
             {"middleName", "L"}, _
             {"surname", "Henderson"}, _
             {"address1", "204 Julius Road"}, _
             {"address2", "Bristol"}, _
             {"address3", ""}, _
             {"address4", ""}, _
             {"address5", ""}, _
             {"postcode", "BS7 8EU"}, _
             {"requestReference", "My Unique Reference"}, _
             {"alias", "true"}, _
             {"fuzzy", "true"}, _
             {"strictMiddle", "false"} _
            }
            Dim response = New CountyCourtJudgementService().MakeRequest(parameters)
            Dim matches = TryCast(response("matches"), Object())
 
            Debug.WriteLine("Found " & matches.Length & " possible matches")
            For Each match As IDictionary(Of String, Object) In matches
                Debug.WriteLine(match("name"))
            Next
        End Sub
 
        Public Function MakeRequest(parameters As IDictionary(Of String, String)) As IDictionary(Of String, Object)
            Dim httpWebRequest = BuildRequest(parameters)
            Try
                Using httpWebResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
                    Dim jsonResponse = ReadResponseToText(httpWebResponse)
                    Debug.WriteLine("Received json from the server:")
                    Debug.WriteLine(jsonResponse)
                    Return New JavaScriptSerializer().Deserialize(Of IDictionary(Of String, Object))(jsonResponse)
                End Using
            Catch ex As WebException
                Throw New CountyCourtJudgementServiceException(If(ReadResponseToText(ex.Response), ex.Message), ex)
            End Try
        End Function
 
        Private Function ReadResponseToText(response As WebResponse) As String
            If response Is Nothing Then
                Return Nothing
            End If
            Using responseStream = New StreamReader(response.GetResponseStream())
                Return responseStream.ReadToEnd()
            End Using
        End Function
 
        Private Function BuildRequest(parameters As IDictionary(Of String, String)) As HttpWebRequest
            ' This is a live API, please contact us if you need to perform load, volume or security testing
            Dim rawRequestUrl = New Uri("https://api.tracesmart.co.uk/ccjsimple/v1/ccj" & ToQueryString(parameters))
            Dim normalizedUrl As String
            Dim requestParameters As String
            Dim oAuth = New OAuthBase()
            Dim signature = oAuth.GenerateSignature(rawRequestUrl, ConsumerKey, ConsumerSecret, Nothing, Nothing, "POST", _
             oAuth.GenerateTimeStamp(), oAuth.GenerateNonce(), OAuthBase.SignatureTypes.HMACSHA1, normalizedUrl, requestParameters)
            Dim requestUrl = New Uri(normalizedUrl)
            Dim httpRequest = DirectCast(System.Net.WebRequest.Create(requestUrl), HttpWebRequest)
            httpRequest.Method = "POST"
            httpRequest.ContentType = "application/x-www-form-urlencoded"
            Using requestStream = New StreamWriter(httpRequest.GetRequestStream())
                requestStream.Write(requestParameters & "&oauth_signature=" & OAuthBase.UrlEncode(signature))
            End Using
 
            Return httpRequest
        End Function
 
        Private Function ToQueryString(parameters As IDictionary(Of String, String)) As String
            Return "?" & String.Join(
                "&", New List(Of String)(parameters.Keys).ConvertAll(
                    Function(key) String.Format(
                        "{0}={1}", OAuthBase.UrlEncode(key), OAuthBase.UrlEncode(parameters(key))
                    )
                ).ToArray()
            )
        End Function
    End Class
 
    Public Class CountyCourtJudgementServiceException
        Inherits Exception
        Public Sub New(message As String, innerException As Exception)
            MyBase.New(message, innerException)
        End Sub
    End Class
End Namespace
 

An example of code that might make use of the previous class is listed below:

´╗┐Public Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ccj = New Tracesmart.VBNet.Api.CountyCourtJudgementService()
        ccj.MakeTestRequest()
    End Sub
 
End Class

From this juncture you should refer to the higher level documentation which explains how to send a request.