VBNet KeepAlive and Logout
Hi Here is the Code for KeepAlive and Logout for VBNet – The problem with previous attempts was to just get a png image instead of the information returned in response
For some strange reason Web Requests have to first be assigned to a HttpWebRequest in order the get the .Accept element to work then all is well
The Only difference with KeepAlive and LogOut is the URL endpoint so have just put them in one sub
I’m Sure you may use in a different way but in any case its straightforward to play with (now lol)
In Case you haven’t done so already you need
Imports System.Net '~~>Needed for Web requests
And Finally some Examples
Now I can sleep as well now Rich lol 
Dave
Hi Here is the Code for KeepAlive and Logout for VBNet – The problem with previous attempts was to just get a png image instead of the information returned in response
For some strange reason Web Requests have to first be assigned to a HttpWebRequest in order the get the .Accept element to work then all is well
The Only difference with KeepAlive and LogOut is the URL endpoint so have just put them in one sub
I’m Sure you may use in a different way but in any case its straightforward to play with (now lol)
In Case you haven’t done so already you need
Imports System.Net '~~>Needed for Web requests
Code:
Private Sub KeepAliveOrKill(SessToken As String, _
Optional AppKey As String = "", _
Optional IsKeepAlive As Boolean = True)
Dim strUrl As String = "" '~~> Holds Keep Alive Or LogOut String
Dim htRequest As HttpWebRequest = Nothing '~~~> *NOTE* HttpWebRequest NOT WebRequest then Convert
Dim htResponse As Net.HttpWebResponse = Nothing '~~~> *NOTE* HttpWebResponse NOT WebResponse
Dim oResponseStream As IO.StreamReader = Nothing '~~> Get the Text Stream
Dim strResponseText As String = "" '~~> Holds the Response message for display if required
Dim strMsg As String = "" '~~> Which one was used Keep or Kill?
Try
If IsKeepAlive = True Then '~~> Keep Alive If arg is set to True (Default)
strUrl = "https://identitysso.betfair.com/api/keepAlive"
strMsg = "KEEP ALIVE" & vbCrLf '~~> For Display later
Else '~~> Log Out if arg is set to false
strUrl = "https://identitysso.betfair.com/api/logout"
strMsg = "LOGGED OUT" & vbCrLf '~~> For Display later
End If 'Keep or Kill
htRequest = WebRequest.Create(strUrl) '~~~> *NOTE* Swap back HttpWebRequest to WebRequest to use Accept (strange)!
With htRequest '~~> The .Accept doo dah will now work
.Method = "GET" '~~> I believe we use Get if we are just retrieving and Post if putting something? both work
.ContentType = "application/json" '~~> Normal
.Accept = "application/json" '~~> This is the cause of the recent problems returning a png (needs Http request)
'~~> Add Headers <~~'
.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8")
.Headers.Add("X-Authentication", SessToken) '~~> Mandatory
If AppKey <> "" Then '~~> Use the Current AppKey (Product) Not mandatory but advisable I suppose
.Headers.Add("X-Application", AppKey)
End If 'AppKey
End With 'htRequest
'~~> Get the response.<~~'
htResponse = htRequest.GetResponse
If htResponse.StatusCode = Net.HttpStatusCode.OK Then '~~> Proceed to get the Response details
oResponseStream = New IO.StreamReader(htResponse.GetResponseStream()) '~~> Convert to text
strResponseText = oResponseStream.ReadToEnd() '~~> Read all the Text to the String Variable
Else '~~> Get outa here
Exit Sub
End If 'Ht Error Check
'~~> Successful Response Details for Log Text etc if required <~~'
If Not strResponseText.Contains("FAIL") Then '~~> Everything is hunky dory
MsgBox("SUCCESS! " & strMsg & strResponseText, MsgBoxStyle.Exclamation, strMsg) '** Test
Else '~~> Have we forgot something?
MsgBox("ERROR! " & strMsg & strResponseText, MsgBoxStyle.Critical, strMsg) '** Test
End If 'Success or fail
Catch ex As Exception
MsgBox("KeepAliveOrKill Error" & vbCrLf & ex.Message, MsgBoxStyle.Critical)
'~~~Clean Up
htResponse.Close()
htResponse = Nothing
oResponseStream = Nothing
End Try
'~~~Clean Up
htResponse.Close()
oResponseStream = Nothing
End Sub 'KeepAliveOrKill Decide what to do
Code:
'~~> Some Form Examples <~~
KeepAliveOrKill(txtSessionToken.Text) '~~> Default Keep Alive Basic
KeepAliveOrKill(txtSessionToken.Text, , False) '~~> Log Out Basic
KeepAliveOrKill(txtSessionToken.Text, txtLiveKey.Text) '~~> Default Keep Alive With App Key
KeepAliveOrKill(txtSessionToken.Text, txtLiveKey.Text, False) '~~> Log Out With App Key
KeepAliveOrKill(txtSessionToken.Text, txtLiveKey.Text, rbKeepAlive.Checked) '~~> Default Checked Radio Button Value for either (If Unchecked will log out)

Dave


SO Hope one of the Betfair Team or Tech guys can help you here as you seem to be having the same sort of problem but Vice Versa
Comment