I am using some inherited code to access a trading API and get intermittent timeouts at the .GetResponse call.
As its intermittent I did think its resource related - other articles refer to the importance of closing disposable objects, so I have used a Using block around the .GetResponse. Have I dealt with all disposables ?
I'm quite new to Web/Http calls so any help please !
Dim success As Boolean = False
Do Until success = True
Try
Dim request = WebRequest.Create("https://poloniex.com/tradingApi")
Dim nonce As Long = CLng((DateTime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds) + nonceoffset
Dim postData = [String].Format("command={0}&nonce={1}", method, nonce)
postData = postData & params
Dim hmAcSha = New HMACSHA512(Encoding.ASCII.GetBytes(PrivateKey))
Dim messagebyte = Encoding.ASCII.GetBytes(postData)
Dim hashmessage = hmAcSha.ComputeHash(messagebyte)
Dim sign = BitConverter.ToString(hashmessage)
sign = sign.Replace("-", "")
request.Timeout = 5000
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = messagebyte.Length
request.Method = "POST"
request.Headers.Add("Key", PublicKey)
request.Headers.Add("Sign", sign.ToLower())
System.Net.ServicePointManager.Expect100Continue = False
Dim stream As System.IO.Stream
stream = request.GetRequestStream()
stream.Write(messagebyte, 0, messagebyte.Length)
Dim json As String
Using response As WebResponse = request.GetResponse()
Dim postreqreader = New StreamReader(response.GetResponseStream())
json = postreqreader.ReadToEnd()
End Using
success = True
Return json
Catch e As WebException
DebugTextAdd("ALERT! API WebException : " & e.Message, MethodBase.GetCurrentMethod.Name())
If System.Threading.Interlocked.Decrement(maxRetryCount) = 0 Then
DebugTextAdd("ALERT! API call failed after several tries. Return Nothing", MethodBase.GetCurrentMethod.Name())
success = True
Return Nothing
Else
DebugTextAdd("Attempt #" & maxRetryCount.ToString & ", try again", MethodBase.GetCurrentMethod.Name())
End If
Catch e As Exception
DebugTextAdd("ALERT! API exception : " & e.Message, MethodBase.GetCurrentMethod.Name())
If System.Threading.Interlocked.Decrement(maxRetryCount) = 0 Then
DebugTextAdd("ALERT! API call failed after several tries. Return Nothing", MethodBase.GetCurrentMethod.Name())
success = True
Return Nothing
Else
DebugTextAdd("Attempt #" & maxRetryCount.ToString & ", try again", MethodBase.GetCurrentMethod.Name())
End If
Finally
End Try
Loop