1 (edited by timmay 11-12-2013 12:45:55)

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

2

Re: Basic .NET REST Connection Script

Hi Tim,

To revive an old post! I am just starting to look at this again in anger and your code below is potentially very useful so thank you. However, i am getting hung up on the DataContractJsonSerializer from the Serialization.Json library. I just don't seem to have it in Studio 2010. I've run round a few forums which suggest it is under a different library but i just can't find it. Any tips?

LERC Officer
Merseyside BioBank - Local Environmental Records Centre for North Merseyside
www.MerseysideBioBank.org.uk

3

Re: Basic .NET REST Connection Script

Hi Tim,

Just wanted to say thanks for this! We can now download data via a desktop application over the REST services, a major step towards replacing the old SOAP dependent ones. Your cookie code was key! Though i have ended up changing the serializer, due to some weird encoding problems which were probably my fault anyway.

LERC Officer
Merseyside BioBank - Local Environmental Records Centre for North Merseyside
www.MerseysideBioBank.org.uk