Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example code for using Postcoder in VB .NET. For more information on Postcoder visit
' Postcoder example
' Allies Computing Ltd
' Demonstrates UK Address lookup rest web service
' Product URL:
' Technical Specs:
' Example output:
' This demo shows how to perform an address lookup and parse the results into VB.Net objects.
' Out of the box it uses a restricted API Key that limits the search to the postcode NR14 7PZ
' To experiment with different search terms, signup for a free evaluation API/Search key at:
' To explore different searches, Signup for an API/Search Key and assign to searchKey variable
' Alternatively the code below can be pasted into an online c# compiler such as
Imports System
Imports System.Collections.Generic
Imports System.Net
Imports System.Text
Imports System.Xml.Serialization
Public Class PostcoderClient
' address class
Public Class Address
Public organisation As String
Public premise As String
Public dependentstreet As String
Public street As String
Public doubledependentlocality As String
Public dependentlocality As String
Public posttown As String
Public county As String
Public postcode As String
Public summaryline As String
Public Overrides Function ToString() As String
Return summaryline
End Function
End Class
' root element of service output
' essentially an array of addresses
<XmlRootAttribute("Addresses")> _
Public Class Addresses
<XmlElementAttribute("Address")> _
Public Address As List(Of Address)
End Class
' code entry point
Public Shared Sub Main()
' API/search key - MUST be supplied to unlock search,
' if this is blank a restricted key is used which limits search to NR147PZ
Dim searchKey As String = ""
' processing type - for other options see
Dim method As String = "address"
' search string - MUST be supplied, if blank defaults to NR147PZ
Dim searchTerm As String = "NR14 7PZ"
Console.WriteLine("Postcoder VB.Net Client Snippet" & vbNewLine)
If (String.IsNullOrEmpty(searchKey)) Then
' no search key supplied - use the restricted evaluation key
searchKey = "PCW45-12345-12345-1234X"
searchTerm = "NR14 7PZ"
Console.WriteLine("No search-key: using restricted evaluation key - resets search term to NR14 7PZ" & vbNewLine & _
"To obtain a free evaluation key, signup via " & _
"" & vbNewLine _
End If
' format the url
Dim uri As String = String.Format("{0}/{1}/UK/{2}", _
searchKey, _
method, _
System.Uri.EscapeDataString(searchTerm) _
' for other uri options see
' call the service
Dim request As HttpWebRequest = CType(WebRequest.Create(uri),HttpWebRequest)
request.Method = "GET"
request.Accept = "application/xml" ' xml or json permitted
Using response As HttpWebResponse = CType(request.GetResponse(),HttpWebResponse)
' check for call failure
If (response.StatusCode <> HttpStatusCode.OK) Then ' = HTTP 200 code (OK)
Throw New ApplicationException(String.Format("Failed : HTTP error code : {0}", response.StatusCode))
End If
' grab the response
Dim addresses As Addresses = New XmlSerializer(GetType(Addresses)).Deserialize(response.GetResponseStream())
' trivial output - display list of address summaries to console
Console.WriteLine(String.Format("Results for: {0}", searchTerm))
If addresses.Address IsNot Nothing Then
For Each address As Address In addresses.Address
End If
End Using
Catch e As Exception
End Try
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.