Streaming API with VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GeorgeDev
    Junior Member
    • Apr 2025
    • 3

    #1

    Streaming API with VB.NET

    Hi!

    I have used the book "Programming for Betfair" by James Buttler to create my program in VB.Net. Everything works fine with the API-NG. I have been trying for a while now to implement the Streaming API. I know that there are SWAGGER / C# samples available, and I had a look at these, but it led to a complexity in the code, which is a bit beyond my programming skills.

    I am using the following function without any problems with the API-NG :

    Public Function SendSportsReq(ByVal jsonString As String)
    Try

    Dim testJsonString as String = ""

    If testJsonString <> "" then
    jsonString = testJsonString
    End if

    Dim request As HttpWebRequest = WebRequest.Create("https://api.betfair.com/exchange/betting/json-rpc/v1")

    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(jsonString)

    Dim responseFromServer As String = ""

    request.Method = "POST"
    request.ContentType = "application/json"
    request.ContentLength = byteArray.Length
    request.Headers.Add("X-Application: APP_KEY")
    request.Headers.Add("X-Authentication: " & SESSION_TOKEN)
    request.AutomaticDecompression = DecompressionMethods.GZip OrElse DecompressionMethods.Deflate
    request.ServicePoint.Expect100Continue = False
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    request.Timeout = 2000

    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)

    Dim response As WebResponse = request.GetResponse()
    dataStream = response.GetResponseStream()

    Dim reader As New StreamReader(dataStream)
    responseFromServer = reader.ReadToEnd()

    reader.Dispose()
    dataStream.Dispose()
    response.Dispose()
    Return responseFromServer

    Catch e As WebException

    Debug.Print("SendSportsReq Error: " & e.Message & " - " & e.StackTrace.ToString & " - " & e.Source.ToString & " - " & e.TargetSite.ToString)
    Return "Error:" & e.Message

    End Try

    End Function

    I have tried to update/change the Function to get it running with the Streaming API:


    Public Function SendStreamReq(ByVal jsonString As String)
    Try

    Dim testJsonString as String = "{""op"":""authentication"", ""id"": 1, ""appKey"":""APP_KEY"", ""session"": ""SESSION_TOKEN""}" & vbCrLf

    If testJsonString <> "" then
    jsonString = testJsonString
    End if

    Dim request As HttpWebRequest = WebRequest.Create("https://stream-api.betfair.com:443")

    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(jsonString)

    Dim responseFromServer As String = ""

    request.Method = "POST"
    request.ContentType = "application/json"
    request.ContentLength = byteArray.Length
    request.Headers.Add("X-Application: APP_KEY")
    request.Headers.Add("X-Authentication: " & SESSION_TOKEN)
    request.AutomaticDecompression = DecompressionMethods.GZip OrElse DecompressionMethods.Deflate
    request.ServicePoint.Expect100Continue = False
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    request.Timeout = 2000

    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)

    Dim response As WebResponse = request.GetResponse()
    dataStream = response.GetResponseStream()

    Dim reader As New StreamReader(dataStream)
    responseFromServer = reader.ReadToEnd()

    reader.Dispose()
    dataStream.Dispose()
    response.Dispose()
    Return responseFromServer

    Catch e As WebException

    Debug.Print("SendSportsReq Error: " & e.Message & " - " & e.StackTrace.ToString & " - " & e.Source.ToString & " - " & e.TargetSite.ToString)
    Return "Error:" & e.Message

    End Try

    End Function

    I have tried the "SendStreamReq"
    - several variants of the testJsonString
    - with and without the request.Headers.Add("X-Application: APP_KEY") and request.Headers.Add("X-Authentication: " & SESSION_TOKEN)
    - the live stream API "stream-api.betfair.com" and also the integration-API
    - with the vbCrLf and without

    It always ends up with an error in the "Dim response As WebResponse = request.GetResponse()" line. The error message is "Received an invalid status line: '{"op":"connection","connectionId":"209-130925203907-1936646"}'."

    Any help would be very much appreciated

    Thanks

    George

  • bfexplorer
    Senior Member
    • Sep 2018
    • 212

    #2
    My code looks something like this one:

    image.png

    Comment

    • GeorgeDev
      Junior Member
      • Apr 2025
      • 3

      #3
      Originally posted by bfexplorer View Post
      My code looks something like this one:

      image.png
      Thanks a lot! That helped me to find the issue.

      Comment

      Working...
      X