不久前,我在 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