Code Samples

Included below are example for C#, PHP and VB.NET.

Basic Authentication in C#

Below is an example of how to pass the HTTP Basic Authentication headers in C# and retrieve basic postcode information. Json.NET has been used to retrieve values from the returned JSON, but you may use whatever JSON library is preferable for you.

//Add these three lines to your "using" block at the top of your code file.
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

//The remaining code sends an APIrequest, and queries one field from the result.
using (var wc = new WebClient())
{
  wc.Credentials = new NetworkCredential(
    "YOUR_API_KEY_HERE", 
    "YOUR_API_PASSWORD_HERE"
  );
  var url = "https://www.streetcheck.co.uk/api/v1/postcode/SN55RF";
  var rawHtml = wc.DownloadString(url);
  var json = (JObject)JsonConvert.DeserializeObject(rawHtml);
  if ((bool)json["success"])
  {
    var resultJson = json["result"];
    if (resultJson != null && resultJson.Type != JTokenType.Null)
    {
      // You can then use the JSON as you wish
      // here for example we pull out the street name
      var street = resultJson["street"].ToString();
    }
  }
}

Basic Authentication in PHP

Below is an example of how to pass the HTTP Basic Authentication headers in PHP and retrieve basic postcode information, using cURL.

<?php
$login = "YOUR_API_KEY_HERE";
$password = "YOUR_API_PASSWORD_HERE";
$url = "https://www.streetcheck.co.uk/api/v1/postcode/SN55RF";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if(curl_error($ch) || $http_status != 200)
{
  //Note: if you get cURL error 60 (unable to get local issuer certificate) then you need to set up your PHP installation with appropriate SSL root certificates
  //See https://stackoverflow.com/questions/29822686/curl-error-60-ssl-certificate-unable-to-get-local-issuer-certificate
  $curl_errno = curl_errno($ch);
  echo "cURL error: " . curl_error($ch) . "<br />";
  echo "cURL HTTP status: " . $http_status . "<br />";
  echo "cURL error number: " . $curl_errno;
}
else 
{
  // You can then use the JSON as you wish
  // here for example we pull out the street name
  $json = json_decode($result, true);
  echo $json["result"]["street"];
}
curl_close($ch);

Basic Authentication in VB.NET

Below is an example of how to pass the HTTP Basic Authentication headers in VB.NET and retrieve basic postcode information. Json.NET has been used to retrieve values from the returned JSON, but you may use whatever JSON library is preferable for you.

Using wc As New System.Net.WebClient        
  wc.Credentials = new System.Net.NetworkCredential(            
    "YOUR_API_KEY_HERE", 
    "YOUR_API_PASSWORD_HERE"
  )
  wc.Encoding = System.Text.Encoding.UTF8
          
  dim url = "https://www.streetcheck.co.uk/api/v1/postcode/SN55RF"
  dim rawHtml = wc.DownloadString(url)
  dim json As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.JsonConvert.DeserializeObject(rawHtml)
  Dim success As Boolean = json.SelectToken("success")
  If success Then
    dim resultJson = json.SelectToken("result")
    If resultJson IsNot Nothing And resultJson.Type <> Newtonsoft.Json.Linq.JTokenType.Null Then
      'You can then use the JSON as you wish - here for example we pull out the street name'
      dim street = resultJson.SelectToken("street").ToString()
    End If
  End If
End Using

Basic Authentication in Java

Below is an example of how to pass the HTTP Basic Authentication headers in Java using HttpClient. There are several variations of this available for different versions of the HttpClient library, see this post for more examples.

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;

HttpClient client = new HttpClient();         
client.getState().setCredentials(
        new AuthScope("www.streetcheck.co.uk", 443),
        new UsernamePasswordCredentials("YOUR_API_KEY_HERE", "YOUR_API_PASSWORD_HERE")
        );         

GetMethod get = new GetMethod("https://www.streetcheck.co.uk/api/v1/postcode/SN55RF");
get.setDoAuthentication( true );
         
try {
    int status = client.executeMethod( get );
             
    // print the status and raw JSON response
    System.out.println(status + "\n" + 
            get.getResponseBodyAsString());
             
} finally {
    // release any connection resources used by the method
    get.releaseConnection();
}