Hi guys,
thanks for the answers, but this actually works out of the box in .NET, there is absolutely no need to set cookies.
I am now using the new httpClient in .Net 4.5
I have created the service layer objects using XamaSofts JSOn to object convertor using cut and paste of the Bonita examples, are from a network monitor. Sure would be much nice if Bonita would provide decent schemas. Also, they really need to clean up their web api, the naming conventions (or more precisly complete lack thereof) are a mess.
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports UZ.BPMN.Datatypes
Imports Newtonsoft.Json
Public Class BPMEngineService
Inherits HttpClient
Private username, password As String
Public Sub New(username As String, password As String)
DefaultRequestHeaders.Accept.Clear()
DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue(“application/json”))
Me.BaseAddress = New Uri("http://localhost:9080")
Me.username = username
Me.password = password
End Sub
Public Function GetTasks(procs As BPMNProcess()) As BPMNTask()
Dim result As BPMNTask() = Nothing
Try
Login()
Dim url As String = “/bonita/API/bpm/humanTask?p=0&c=10&o=priority%20DESC&f=state%3dready&f=user_id%3d0”
Dim response As HttpResponseMessage = GetAsync(url).Result
Dim reponseData As String = response.Content.ReadAsStringAsync().Result
HttpContext.Current.Trace.Write(response.ToString)
HttpContext.Current.Trace.Write(reponseData)
result = JsonConvert.DeserializeObject(reponseData, GetType(BPMNTask()))
Catch ex As Exception
HttpContext.Current.Trace.Write("Error contacting REST service: " & ex.ToString)
Finally
Logout()
End Try
Return result
End Function
Public Function GetProcess(processID As String) As BPMNProcess
Dim result As BPMNProcess = Nothing
Try
Login()
Dim url As String = “/bonita/API/bpm/process/” & processID
Dim response As HttpResponseMessage = GetAsync(url).Result
Dim reponseData As String = response.Content.ReadAsStringAsync().Result
result = JsonConvert.DeserializeObject(reponseData, GetType(BPMNProcess))
Catch ex As Exception
HttpContext.Current.Trace.Write("Error contacting REST service: " & ex.ToString)
Finally
Logout()
End Try
Return result
End Function
Public Function StartCase(processID As String) As BPMNCase
Dim result As BPMNCase = Nothing
Try
Login()
Dim formContent As New List(Of KeyValuePair(Of String, String)) From {New KeyValuePair(Of String, String)(“processDefinitionId”, processID)}
Dim url As String = “/bonita/bpm/case/”
Dim response As HttpResponseMessage = MyBase.PostAsync(url, New FormUrlEncodedContent(formContent)).Result
HttpContext.Current.Trace.Write(response.ToString)
HttpContext.Current.Trace.Write(response.Headers.ToString())
Dim reponseData As String = response.Content.ReadAsStringAsync().Result
result = JsonConvert.DeserializeObject(reponseData, GetType(BPMNCase))
Catch ex As Exception
HttpContext.Current.Trace.Write("Error contacting REST service: " & ex.ToString)
Finally
Logout()
End Try
Return result
End Function
Public Function GetProcesses() As BPMNProcess()
Dim result As BPMNProcess() = Nothing
Try
Login()
Dim url As String = “/bonita/API/bpm/process?p=0&c=10”
Dim response As HttpResponseMessage = GetAsync(url).Result
Dim reponseData As String = response.Content.ReadAsStringAsync().Result
HttpContext.Current.Trace.Write(response.ToString)
HttpContext.Current.Trace.Write("Process data", reponseData)
result = JsonConvert.DeserializeObject(reponseData, GetType(BPMNProcess()))
Catch ex As Exception
HttpContext.Current.Trace.Write("Error contacting REST service: " & ex.ToString)
End Try
Return result
End Function
Private Sub Login()
Try
'Add form data
Dim formContent As New List(Of KeyValuePair(Of String, String)) From {New KeyValuePair(Of String, String)(“username”, username),
New KeyValuePair(Of String, String)(“password”, password)}
Dim response As HttpResponseMessage = MyBase.PostAsync("/bonita/loginservice", New FormUrlEncodedContent(formContent)).Result
'Display as string
HttpContext.Current.Trace.Write(response.ToString)
HttpContext.Current.Trace.Write(response.Headers.ToString())
Catch ex As Exception
HttpContext.Current.Trace.Write("Error contacting REST service: " & ex.ToString)
End Try
End Sub
Private Sub Logout()
Try
'Add form data
Dim response As HttpResponseMessage = MyBase.GetAsync(“/bonita/logoutservice”).Result
'Display as string
HttpContext.Current.Trace.Write(response.ToString)
HttpContext.Current.Trace.Write(response.Headers.ToString())
Catch ex As Exception
HttpContext.Current.Trace.Write("Error contacting REST service: " & ex.ToString)
End Try
End Sub
Public Overrides Function SendAsync(request As HttpRequestMessage, cancellationToken As Threading.CancellationToken) As Threading.Tasks.Task(Of HttpResponseMessage)
Dim sb As New StringBuilder
For i As Integer = 0 To request.Properties.Count - 1
sb.AppendLine(request.Properties.Keys(i) & ": " & request.Properties.Values(i).ToString)
Next
HttpContext.Current.Trace.Write(“BPMNEngine Request”, sb.ToString)
Dim result As Threading.Tasks.Task(Of HttpResponseMessage) = MyBase.SendAsync(request, cancellationToken)
Return result
End Function
End Class