1

不久前,我在 VB.NET 中编写了一个程序来使用 Betfair Exchange API。它已经完美运行了几个月,但在周二一夜之间它停止了工作。我仍然可以登录,但从周三开始,我无法从服务器获取任何其他信息。

必发正在调查,但据他们说,似乎没有其他人遇到同样的问题——尽管我不确定有多少人会使用 VB.NET。

下面是我用来从 API 获取数据的函数。就像我说的那样,它在周二晚上开始工作,但从周三早上开始就不行了。这里有什么“不完美”或“可能更好”,或者我可以尝试一些替代代码吗?或者我的电脑上可能发生了什么导致问题的原因?

该程序在“dataStream = request.GetRequestStream()”这一行发生故障。错误是“从传输流接收到意外的 EOF 或 0 个字节”。

如果有人能提供任何建议,我将不胜感激。谢谢!

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
4

2 回答 2

1

我会检查提供商最近是否没有弃用 TLS 1.0(事实上,他们现在应该这样做)。

如果是这样,您的代码需要强制使用 TLS 1.1+:

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

这只需设置一次,通常在(静态)类型初始化程序或类似的。

我 100% 同意 Andrew Mortimer 的观点,即您应该Using尽可能使用积木。我还建议将所有字符串值移动到变量或常量中,以清理并保持可维护性。例如:

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

更新

我刚刚在他们的网站上发现了这个公告:

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

于 2020-12-04T18:05:49.420 回答
0

如果您被允许在此项目中使用外部依赖项,我建议您使用 RestSharp nuget 包,它非常适合创建 API 请求并获得响应,而无需使用会变得混乱的 httpclient。

链接:https ://restsharp.dev/

于 2020-12-11T12:08:44.963 回答