Topic: Basic .NET REST Connection Script
In case anyone is struggling with getting VB.NET to work with the new REST web services, I've attached what I have done below. I've also added a function that will return species records for a given grid reference. It should be straightforward enough to convert to C# if you're that way inclined.
Cheers,
Tim
Imports System.Net
Imports System.Runtime.Serialization.Json
Private Function GetTaxonObservation(UserName As String, Password As String, GridReference As String) As List(Of TaxonObservation)
Dim cc As New CookieContainer()
Dim LoginRequest As HttpWebRequest = HttpWebRequest.Create("https://data.nbn.org.uk/api/user/login?username=" & UserName & "&password=" & Password & "&remember=false")
LoginRequest.CookieContainer = cc
Dim LoginResponse As HttpWebResponse = LoginRequest.GetResponse()
If LoginResponse.StatusCode = HttpStatusCode.OK AndAlso LoginResponse.Cookies.Count > 0 Then
'Log in
Dim c As Cookie = LoginResponse.Cookies(0)
Dim DataRequest As HttpWebRequest = HttpWebRequest.Create("https://data.nbn.org.uk/api/taxonObservations?gridRef=" & GridReference)
Dim cc2 As New CookieContainer()
cc2.Add(New Cookie(c.Name, c.Value, c.Path, c.Domain))
DataRequest.CookieContainer = cc2
'Get data
Dim wr As HttpWebResponse = DataRequest.GetResponse()
Dim js As New DataContractJsonSerializer(GetType(List(Of TaxonObservation)))
Dim d As List(Of TaxonObservation) = CType(js.ReadObject(wr.GetResponseStream()), List(Of TaxonObservation))
'Log out
Dim LogoutRequest As HttpWebRequest = HttpWebRequest.Create("https://data.nbn.org.uk/api/user/logout")
Dim cc3 As New CookieContainer()
cc3.Add(New Cookie(c.Name, c.Value, c.Path, c.Domain))
LogoutRequest.CookieContainer = cc3
Dim LogoutResponse As HttpWebResponse = LogoutRequest.GetResponse()
Return d
End If
Return Nothing
End Function
Public Class TaxonObservation
Public Property observationID As Integer
Public Property fullVersion As Boolean
Public Property datasetKey As String
Public Property surveyKey As String
Public Property sampleKey As String
Public Property observationKey As String
Public Property siteKey As String
Public Property siteName As String
Public Property featureID As Integer
Public Property location As String
Public Property resolution As String
Public Property taxonVersionKey As String
Public Property pTaxonVersionKey As String
Public Property pTaxonName As String
Public Property pTaxonAuthority As String
Public Property startDate As String
Public Property endDate As String
Public Property recorder As String
Public Property sensitive As Boolean
Public Property absence As Boolean
Public Property dateTypekey As String
End Class