vb.netexchange-serverwebrequestbetfair

Why would my VB.NET WebRequest suddenly stop working?


A while ago I wrote a programme in VB.NET to use the Betfair Exchange API. It has worked perfectly for months, but overnight on Tuesday it stopped working. I can still log in, but from Wednesday I have been unable to get anything else from the server.

Betfair are investigating, but according to them nobody else seems to be experiencing the same problem - although I'm not sure how many will be using VB.NET.

Below is the function I have been using to obtain data from the API. Like I said it was working on Tuesday night but not from Wednesday morning. Is there anything here which is "not perfect" or "could be better", or perhaps there is some alternative code I could try? Or is there something which might have happened on my pc which has caused the problem?

The programme falls over at the line "dataStream = request.GetRequestStream() ". The error is "Received an unexpected EOF or 0 bytes from the transport stream."

I would be grateful for any advice that anyone could offer. Thank you!

Public Function CreateRequest(ByVal postData As String, Optional ByVal accountsApi As Boolean = False)
    Dim Url As String = "https://api.betfair.com/exchange/betting/json-rpc/v1"
    If accountsApi Then Url = "https://api.betfair.com/exchange/account/json-rpc/v1"
    Dim request As WebRequest = Nothing
    Dim dataStream As Stream = Nothing
    Dim response As WebResponse = Nothing
    Dim strResponseStatus As String = ""
    Dim reader As StreamReader = Nothing
    Dim responseFromServer As String = ""
    Try
        request = WebRequest.Create(New Uri(Url))
        request.Method = "POST"
        request.ContentType = "application/json-rpc"
        request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8")
        request.Headers.Add("X-Application", appKey)
        request.Headers.Add("X-Authentication", sessToken)
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) ' Data to post such as ListEvents, ListMarketCatalogue etc
        request.ContentLength = byteArray.Length ' Set the ContentLength property of the WebRequest.
        dataStream = request.GetRequestStream() ' Get the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length) ' Write the data to the request stream.
        dataStream.Close() ' Close the Stream object.
        response = request.GetResponse() ' Get the response.
        strResponseStatus = CType(response, HttpWebResponse).StatusDescription ' Display the status below if required
        dataStream = response.GetResponseStream() ' Get the stream containing content returned by the server.
        reader = New StreamReader(dataStream) ' Open the stream using a StreamReader for easy access.
        responseFromServer = reader.ReadToEnd() ' Read the content.
        reader.Close() : dataStream.Close() : response.Close()
    Catch ex As Exception
        MsgBox("CreateRequest Error" & vbCrLf & ex.Message, MsgBoxStyle.Critical, " Error")
    End Try
    Return responseFromServer
End Function

Solution

  • I would check that the provider hasn't recently deprecated use of TLS 1.0 (as they should have done before now, in fact).

    If so, your code needs to enforce use of TLS 1.1+:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    

    This only has to be set once, usually in the (static) type initializer or similar.

    And I 100% agree with Andrew Mortimer that you should use Using blocks wherever possible. I'd also suggest moving all of your string values into variables or constants to clean things up and keep them maintainable. Eg:

    Const ContentType As String = "application/json-rpc"
    ...
    request.ContentType = ContentType
    

    UPDATE

    I just found this announcement on their site:

    https://forum.developer.betfair.com/forum/developer-program/announcements/33563-tls-1-0-no-longer-supported-from-1st-december-all-betfair-api-endpoints